The value “(string)” is invalid

2019-05-03 11:07发布

问题:

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?

回答1:

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!



回答2:

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



回答3:

Please try using

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; }
}


回答4:

It does not work because your domain class has property named Review (public string Review { get; set; }) and method that is receiving updated domain class has parameter named review also (public ActionResult Create(MovieReview review)).

Change public ActionResult Create(MovieReview review) to public ActionResult Create(MovieReview movieReview).



回答5:

I think in below model.Review and Model.Rating can't be directly accessible as your Mode is IEnumerable

     <th>
        @Html.DisplayNameFor(model => model.Rating)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Review)
    </th>


回答6:

your var name should not be same as a class name try this :

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

    [Range(1, 10)]
    [Required]
    public int ratingId { get; set; }
    public string reviewText { get; set; }
    public int movieId1 { get; set; }
}


回答7:

Remove the @Html.ValidationSummary(true)

or

remove all ValidationMessageFor and change for(remove true) @Html.ValidationSummary()

Edit:

You want to be remove all ValidationMessageFor and change the ValidationSummary(true) to ValidationSummary()