0

我有一个非常复杂的模型和一个视图(如下)。我只有一个字段为必填项(Key Active Mg)。当我标记它[Required(ErrorMessage="Key Active Mg is required")]@Html.ValidationMessageFor为该字段设置时,我的应用程序将不让我输入任何内容并单击保存。当然Model.IsValid会回来false。它不会返回并用红色勾勒出该字段,表明它是必需的。有谁知道为什么?

我的模型:

    public class RawValues
    {
        [Key]
        public int Pk { get; set; }

        public int? FillerFk { get; set; }
        [Display(Name = "Filler")]
        [ForeignKey("FillerFk")]
        public virtual Filler Filler { get; set; }

        public int? CapsuleFk { get; set; }
        [Display(Name = "Capsule")]
        [ForeignKey("CapsuleFk")]
        public virtual Capsule Capsule { get; set; }

        public int? KeyActiveFk { get; set; }
        [Display(Name = "Key Active")]
        [ForeignKey("KeyActiveFk")]
        public virtual KeyActive KeyActive { get; set; }

        [Display(Name="API #1")]
        public int? Active1 { get; set; }

        [Display(Name = "API #2")]
        public int? Active2 { get; set; }

        [Display(Name = "API #3")]
        public int? Active3 { get; set; }

        [Display(Name = "API #4")]
        public int? Active4 { get; set; }

        [Display(Name = "API #5")]
        public int? Active5 { get; set; }

        [Display(Name = "Key Active Mg")]
        [Required(ErrorMessage="Key Active Mg is required.")]
        public int KeyActiveMg { get; set; }

        [Display(Name = "E4M")]
        public bool E4M { get; set; }

        [Display(Name = "K100M")]
        public bool K100M { get; set; }

        public int TimeReleaseFillerFk { get; set; }
        public int NumberCapsules { get; set; }

        public string CreatedBy { get; set; }
        public DateTime CreatedDate { get; set; }
    }

我的观点:

@model CapWorx.QuikCap.Models.RawValues

@* This partial view defines form fields that will appear when creating and editing entities *@

<div data-role="fieldcontain">
    @Html.LabelFor(model => model.Capsule, new { @class = "ui-input-text" })
    @Html.DropDownListFor(m => m.Capsule.Pk, new SelectList(ViewBag.Capsules, "Pk", "Name", "Pk"))
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.Active1)
    @Html.EditorFor(model => model.Active1)
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.Active2)
    @Html.EditorFor(model => model.Active2)
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.Active3)
    @Html.EditorFor(model => model.Active3)
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.Active4)
    @Html.EditorFor(model => model.Active4)
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.Active5)
    @Html.EditorFor(model => model.Active5)
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.KeyActive, new { @class = "ui-input-text" })
    @Html.DropDownListFor(m => m.KeyActive.Pk, new SelectList(ViewBag.KeyActives, "Pk", "Name", "Pk"))
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.KeyActiveMg)
    @Html.EditorFor(model => model.KeyActiveMg)
    @Html.ValidationMessageFor(model => model.KeyActiveMg)
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.E4M)
    @Html.DropDownListFor(x => x.E4M,
                              new[] { 
                                    new SelectListItem() { Text = "Off", Value = "False", Selected = true }, 
                                    new SelectListItem() { Text = "On", Value = "True" } },
                              new { data_role = "slider" })
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.K100M)
    @Html.DropDownListFor(x => x.K100M,
                              new[] { 
                                    new SelectListItem() { Text = "Off", Value = "False", Selected = true }, 
                                    new SelectListItem() { Text = "On", Value = "True" } },
                              new { data_role = "slider" })
</div>
<div data-role="fieldcontain">
    @Html.LabelFor(model => model.Filler, new { @class = "ui-input-text" })
    @Html.DropDownListFor(m => m.Filler.Pk, new SelectList(ViewBag.Fillers, "Pk", "Name", "Pk"))
    @Html.ValidationMessage("FillerName", "Filler is required")
</div>

我的控制器:

[HttpPost]
        public ActionResult Create(RawValues rawvalues)
        {
            rawvalues.CreatedBy = User.Identity.Name;
            rawvalues.CreatedDate = DateTime.Now;
            rawvalues.TimeReleaseFillerFk = Helpers.OperationContext.GetTimeReleaseFillerFk(rawvalues.E4M, rawvalues.K100M);

            rawvalues.CapsuleFk = rawvalues.Capsule.Pk;
            rawvalues.FillerFk = rawvalues.Filler.Pk;
            rawvalues.KeyActiveFk = rawvalues.KeyActive.Pk;

            rawvalues.Filler = Helpers.OperationContext.GetFiller(rawvalues.Filler.Pk);
            rawvalues.Capsule = Helpers.OperationContext.GetCapsule(rawvalues.Capsule.Pk);
            rawvalues.KeyActive = Helpers.OperationContext.GetKeyActive(rawvalues.KeyActive.Pk);

            rawvalues.NumberCapsules = 100;

            var errors = ModelState.Values.SelectMany(v => v.Errors);

            if (ModelState.IsValid) {
                rawvaluesRepository.InsertOrUpdate(rawvalues);
                rawvaluesRepository.Save();
                List<Results> results = Helpers.OperationContext.CallCalculate(rawvalues);
                return View("Results", results);
            } else {
                ViewBag.Error = "Model State was not valid.";
                return View("Error");
            }
        }

我的截图:

在此处输入图像描述

更新

我已将控制器代码更新为以下内容:

    if (ModelState.IsValid) {
        rawvaluesRepository.InsertOrUpdate(rawvalues);
        rawvaluesRepository.Save();
        List<Results> results = Helpers.OperationContext.CallCalculate(rawvalues);
        return View("Results", results);
    } else {
        ViewBag.Capsules = Helpers.OperationContext.GetCapsules();
        ViewBag.Fillers = Helpers.OperationContext.GetFillers();
        ViewBag.KeyActives = Helpers.OperationContext.GetKeyActives();
        return View();
    }

这解决了我的问题。我需要返回相同的视图以在屏幕上显示错误。使用 DataAnnotations Validation,表单实际上确实命中了 Create 的 HttpPost 方法,如果有错误(验证错误)ModelState.IsValid 将返回 false,在这种情况下我需要返回相同的视图。请看下面的截图!

在此处输入图像描述

4

4 回答 4

5

看起来你正在返回另一个视图(错误)如果ModelState.IsValidfalse。您应该将发布的视图模型返回到相同的创建视图。

public ActionResult Create(RawValues model)
{
  if(ModelState.IsValid)
  {
     //everything is good. Lets save and redirect
  }
  return View(model);
}
于 2013-04-01T15:50:03.750 回答
0

我的猜测是您不包括“jquery.unobtrusive-ajax.min.js”和/或“jquery.validate.unobtrusive.min.js”。我相信这些是解释 DataAnnotations 添加的元数据并提供您正在寻找的验证方法所必需的。

于 2013-04-01T15:48:29.927 回答
0

当您的模型无效时,您实际上并没有返回它并带有验证错误。然后,你这样做:

@Html.ValidationMessageFor(model => model.KeyActiveMg)

但没有任何验证消息。您返回了一个带有空白模型的全新视图。相反,您应该将无效模型返回到视图,如下所示:

return View(rawvalues);

更新

您的控制器应如下所示:

public ActionResult Create()
{
    return View(new RawValues());
}

[HttpPost]
public ActionResult Create(RawValues model)
{
    if (ModelState.IsValid)
    {
        // Do stuff
    }
    else
    {
        return View(model);
    }
}

你的观点似乎很好,所以它应该从这里开始。

如果您在谈论客户端验证,则需要引用必要的 JS 文件。它们应该已经在您的 Bundle 配置中进行了预配置。您只需要添加到您的视图中:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jqueryval")

只是为了确保,位于App_Start文件夹中的 BundleConfig 类应该有这些:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/jquery-{version}.js"));

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-{version}.js"));

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.unobtrusive*",
            "~/Scripts/jquery.validate*"));
于 2013-04-01T15:52:32.427 回答
-1

我想你需要这个

[HttpPost]
public ActionResultCreate(RawValues model)
{
 ....

 if (!ModelState.IsValid)
 {
    ModelState.AddModelError("KeyActiveMg", "KeyActiveMg field is required");
    var model = new GetModel();
    return View("Results", model);
 }  

    rawvaluesRepository.InsertOrUpdate(rawvalues);
    rawvaluesRepository.Save();
    List<Results> results = Helpers.OperationContext.CallCalculate(rawvalues);
    return View("Results", results);
}
于 2013-04-01T16:06:24.727 回答