将枚举添加到MVC视图中断返回的模型

时间:2019-06-25 15:14:08

标签: c# asp.net-mvc

我有这个控制器:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ExempKillData kill)
        {
            if (ModelState.IsValid)
            {
                db.Entry(kill).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index","Owners");
            }
            return View(kill);
        }

和这个(长度被截断的)模型:

[Display(Name ="No Data")]
        [StringLength(40)]
        public string NO_DATA { get; set; }

        public DisposalMethods MethodOfDisposal { get; set; }

        public int ID { get; set; }

和此(按长度截取)视图:

                    <td valign="top">
                        @Html.EditorFor(model => kill.SPECIES, null, "SPECIES", new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.SPECIES, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EnumDropDownListFor(model => kill.MethodOfDisposal, null, new { @class = "form-control" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.EXEM_YEAR, null, "EXEM_YEAR", new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.EXEM_YEAR, "", new { @class = "text-danger" })
                    </td>

我尝试了以下两种方法来声明下拉列表:


@Html.EnumDropDownListFor(model => kill.MethodOfDisposal, "MethodOfDisposal", new { @class = "form-control" })

@Html.EnumDropDownListFor(model => kill.MethodOfDisposal, null, new { @class = "form-control" })

如果我注释掉@ Html.EnumDropDownListFor,它会完美地工作-返回到控制器的模型已经非常详细,除了Enum值。

Good model

我取消注释EnumDropDownList时,返回给控制器的模型都是空值。 ...除了MethodOfDisposal。

Null model

...尽管我的数据来自视图上的POST操作,但看起来不错:

Good data

我在做什么错?我希望在视图中添加一个新字段,以不会破坏模型的其余部分。

0 个答案:

没有答案