現在、ユーザーがデータを入力できる 2 つのテキスト ボックスを含むビューがあります。1 つのテキスト ボックスでは 1 ~ 10 の値のみが許可され、もう 1 つのテキスト ボックスでは文字列が許可されます。どのようなコード変更を行ったのかわかりませんが、文字列を受け入れる 2 番目のテキスト ボックスが "機能" しなくなりました。たとえば、文字列を入力してフォームを送信しようとすると、「値 "(文字列)" が無効です」という検証メッセージが表示されます。以下は、私のソリューションのコード スニペットです。
実在物:
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; }
}
コントローラ:
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);
}
部分図:
@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>
}
今の問題は、私が間違っていることは何ですか?その検証エラーが生成されるのはなぜですか?