我剛剛創建了一個新的控制器,連同它的CRUD表單等,以及一個數據庫優先的EF模型/實體。EF數據庫第一次生成的表單將不會生效
它在保存時拋出了一些驗證錯誤,但由於表單有驗證器,我不明白爲什麼會這樣。
由於我無法理解的原因,我沒有得到任何驗證。它正好進入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拋出'required'驗證錯誤,以及sGroupLabel爲什麼拋出長度驗證錯誤。
謝謝
我同意數據註釋將通過解決此問題
,它沒有任何驗證,因此它是一個謎,爲什麼我得到驗證錯誤出提交..但是,爲了回答您的(b)問題,我正在編輯一行,其中填充了一些數據...一個預先存在的行,其中有由另一個進程生成的數據。 – reidLinden
@reidLinden - 有2(或3)級驗證。有客戶端jquery驗證,有服務器端MVC驗證,然後EF驗證它的模型屬性。你在ef模型中使用流暢的映射嗎?你是否告訴它一些領域是必需的? –
當談到asp.net/mvc的東西時,我非常生硬......所以我甚至不能說我缺乏對Fluent等術語的意識意味着我不是......我可能會這樣。我所知道的是我已經從現有的數據庫生成我的EF層。我發現最大長度的錯誤。我更新了表,但忘了重新運行模型刷新...但它仍然抱怨sInitials ..我唯一的想法是,不知何故,因爲該列有一個NOT NULL約束,(與默認的''值),當空字段返回到服務器時,或者您提到的EF驗證層時,空值計爲空值。 – reidLinden