Ich bin neu in MVC. Ich habe Probleme mit der Validierung Fehler, da ich alle Datenbank-Feld überprüft habe ich kein Problem dort und immer noch ich Ich erhalte den Fehler, dass die Validierung für eine oder mehrere Entitäten fehlgeschlagen ist. Ich kann den Validierungsfehler nicht finden.Validierung fehlgeschlagen für eine oder mehrere Entitäten bei der Verwendung von ASP.NET MVC und Razor
Hier ist meine Datenbankschema:
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; }
}
hier meiner Meinung nach ist:
@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);
}
Untersuchen Sie 'ModelState', um festzustellen, welche Validierungsfehler Sie haben. Beachten Sie auch, dass Sie nirgends den Wert der Eigenschaft imagePath vor dem Speichern des Modells setzen. –