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?
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!
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
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; }
}
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)
.
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>
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; }
}
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()
来源:https://stackoverflow.com/questions/16641418/the-value-string-is-invalid