EF数据库优先生成的表单不会验证

时间:2012-05-03 18:00:24

标签: asp.net-mvc entity-framework

我刚刚创建了一个新的控制器,以及它的CRUD表单等,以及数据库优先的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会抛出“必需的”验证错误,以及为什么sGroupLabel会抛出长度验证错误,这让我感到困惑。

由于

2 个答案:

答案 0 :(得分:2)

a)您的模型没有数据验证注释。因此,MVC不会进行任何验证,因为您没有告诉它要验证什么。

b)您没有提到您提交的内容。你刚才提交一份空表格吗?

答案 1 :(得分:0)

似乎数据注释将解决此问题

   [Required(AllowEmptyStrings = true)]
   [DisplayFormat(ConvertEmptyStringToNull = false)]
   public object Note { get; set; }

通过http://fendy-huang.blogspot.com/2011/04/how-to-pass-empty-string-in-entity.html