我刚刚使用数据库优先的 EF 模型/实体创建了一个新控制器及其 CRUD 表单等。
它在保存时抛出了许多验证错误,但由于表单有验证器,我不明白为什么会这样。
由于我无法理解的原因,我根本没有得到任何验证。它直接进入 saveChanges() 调用,该调用立即失败。
这是编辑表格:
@model StatementsApplication.DAL.StatementTask
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>StatementTask</legend>
<div class="editor-label">
@Html.LabelFor(model => model.sInitials)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sInitials)
@Html.ValidationMessageFor(model => model.sInitials)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.dtCompleted)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.dtCompleted)
@Html.ValidationMessageFor(model => model.dtCompleted)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.sGroupLabel)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sGroupLabel)
@Html.ValidationMessageFor(model => model.sGroupLabel)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.nGroupSequence)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.nGroupSequence)
@Html.ValidationMessageFor(model => model.nGroupSequence)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.sTaskType)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sTaskType)
@Html.ValidationMessageFor(model => model.sTaskType)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.sTaskLabel)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sTaskLabel)
@Html.ValidationMessageFor(model => model.sTaskLabel)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.nTaskSequence)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.nTaskSequence)
@Html.ValidationMessageFor(model => model.nTaskSequence)
</div>
@Html.HiddenFor(model => model.ID)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
这是生成的模型:
namespace StatementsApplication.DAL
{
using System;
using System.Collections.Generic;
public partial class StatementTask
{
public int StmtBatchID { get; set; }
public string sInitials { get; set; }
public Nullable<System.DateTime> dtCompleted { get; set; }
public string sGroupLabel { get; set; }
public double nGroupSequence { get; set; }
public string sTaskType { get; set; }
public string sTaskLabel { get; set; }
public double nTaskSequence { get; set; }
public int ID { get; set; }
public virtual StatementBatch tblStmtBatch { get; set; }
}
}
这是控制器位...
//
// GET: /StatementTask/Edit/5
public ActionResult Edit(int id = 0)
{
StatementTask statementtask = db.StatementTasks.Find(id);
if (statementtask == null)
{
return HttpNotFound();
}
ViewBag.StmtBatchID = new SelectList(db.StatementBatches, "ID", "sStatus", statementtask.StmtBatchID);
return View(statementtask);
}
//
// POST: /StatementTask/Edit/5
[HttpPost]
public ActionResult Edit(StatementTask statementtask)
{
if (ModelState.IsValid)
{
try
{
db.Entry(statementtask).State = EntityState.Modified;
db.SaveChanges();
}
catch (Exception ex) {
throw ex;
}
return RedirectToAction("Index");
}
ViewBag.StmtBatchID = new SelectList(db.StatementBatches, "ID", "sStatus", statementtask.StmtBatchID);
return View(statementtask);
}
对于我来说,为什么 sInitials 会抛出“必需的”验证错误,以及为什么 sGroupLabel 会抛出长度验证错误,这让我有些困惑。
谢谢