Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The value "(string)" is invalid

I currently have a view that contains two text boxes where users can enter in some data. One text box only allows values of 1-10, the other a string. I am not sure what code change I made, but the second text box accepting a string no longer "works". For example, when I enter in a string and try to submit the form, I get a validation message that states "The value "(string)" is invalid. Below are some code snippets in my solution.

Entity:

public class MovieReview
{
    public int Id { get; set; }

    [Range(1, 10)]
    [Required]
    public int Rating { get; set; }
    public string Review { get; set; }
    public int MovieId { get; set; }
}

Controller:

public class ReviewsController : Controller
{
    private MovieLoversDb _db = new MovieLoversDb();

    public ActionResult Index([Bind(Prefix = "id")]int movieId)
    {
        var movie = _db.Movies.Find(movieId);
        if (movie != null)
        {
            return View(movie);
        }
        return HttpNotFound();
    }

    [HttpGet]
    public ActionResult Create(int movieId)
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(MovieReview review)
    {
        if (ModelState.IsValid)
        {
            _db.MovieReviews.Add(review);
            _db.SaveChanges();
            return RedirectToAction("Index", new { id = review.MovieId });
        }
        return View(review);
    }

Partial View:

@model MovieLovers.Models.MovieReview

@{
    ViewBag.Title = "Review a Movie";
}

<h2>Review a Movie</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

<fieldset>
    <legend>New Review</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Rating)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Rating)
        @Html.ValidationMessageFor(model => model.Rating)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Review)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Review)
        @Html.ValidationMessageFor(model => model.Review)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ReviewerName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ReviewerName)
        @Html.ValidationMessageFor(model => model.ReviewerName)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

The question now is, what am I doing wrong? Why is that validation error generating?

like image 932
Ron Saylor Avatar asked May 20 '13 01:05

Ron Saylor


2 Answers

I figured it out using a suggestion by Jasen in comments under the original post. It seems as though "Review" may have been used twice, although I could not find where. I changed the property name to "body" and now it works.

Thanks for all your help!

like image 150
Ron Saylor Avatar answered Oct 08 '22 16:10

Ron Saylor


the problem is the name of the field is review:

@Html.EditorFor(model => model.Review)

as the name of the parameter in the control:

public ActionResult Create(MovieReview review)

Changing the name of the parameter should works too

like image 24
Éderson C. do Nascimento Avatar answered Oct 08 '22 17:10

Éderson C. do Nascimento