2

我刚刚使用数据库优先的 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 会抛出长度验证错误,这让我有些困惑。

谢谢

4

2 回答 2

2

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

b)您没有提及您提交的内容。您只是提交一个空表格吗?

于 2012-05-03T18:05:28.227 回答
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

于 2012-05-03T20:14:46.073 回答