MVCに新しいです.iすべてのデータベースフィールドを確認したため検証エラーが発生しました。 1つまたは複数のエンティティの検証に失敗したというエラーが表示されます。検証エラーを見つけることができません。ここでASP.NET MVCとRazorを使用している場合、1つまたは複数のエンティティの検証が失敗します
は私のデータベーススキーマです:
ここpublic partial class make
{
public int make_id { get; set; }
public int category_id { get; set; }
public string title { get; set; }
public string imagePath { get; set; }
public virtual category category { get; set; }
}
が私の見解です:
@model globalEngineProject.make
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("Home", "make", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>category</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.title, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.title)
@Html.ValidationMessageFor(model => model.title)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.category_id, "category_id", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("category_id", String.Empty)
@Html.ValidationMessageFor(model => model.category_id)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.imagePath, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input name="file" type="file" id="file"/>
@Html.ValidationMessageFor(model => model.imagePath)
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Home([Bind(Include = "make_id,category_id,title")] make make, HttpPostedFileBase file)
{
try
{
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/images/"), fileName);
file.SaveAs(path);
}
db.makes.Add(make);
db.SaveChanges();
}
ViewBag.category_id = new SelectList(db.categories, "category_id", "category_name", make.category_id);
return View(make);
// return View("Index");
}
catch (RetryLimitExceededException)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View(make);
}
'ModelState'を検査して、どの検証エラーが発生しているのかを判断してください。また、モデルを保存する前にプロパティ 'imagePath'の値を設定していないことに注意してください。 –