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?