视图不会在回发时返回整个模型

时间:2019-06-24 20:56:43

标签: c# asp.net-mvc razor

我创建了模型,视图和控制器。在我看来,我遍历的集合,并创建一个列出了每个表的表以及一个保存按钮。

当我单击“保存”按钮时,返回到控制器的唯一数据是ID和LandownerID-所有其他字段显示为空。

我今天大部分时间都在搜索google,并尝试了多个答案,但没有一个起作用。

控制器:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,ExemptionNumber,IssueDate,KillCount,TRAPPING,SHOOTING,DOGS,OTHER,NO_INDICATION,NOTES,SPECIES,E_LANDOWNER,EXEM_YEAR,MethodOfDisposal,NO_DATA")] ExempKillData exempKillData)
        {
            if (ModelState.IsValid)
            {
                db.Entry(exempKillData).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index","Owners");
            }
            return View(exempKillData);
        }

查看:

@model IEnumerable<Exemptions.Models.ExempKillData>

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}



<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <table border="1" id="tbl">
        <tbody>
            ..table headers snipped...
            @foreach (var kill in Model)
            {
                <tr>
                    <td>
                        @Html.EditorFor(model => Model.First().ID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    </td>
                    <td>
                        @Html.EditorFor(model => Model.First().ExemptionNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.IssueDate, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.IssueDate, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.KillCount, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.KillCount, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.TRAPPING, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.TRAPPING, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.SHOOTING, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.SHOOTING, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.DOGS, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.DOGS, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.OTHER, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.OTHER, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.NO_INDICATION, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.NO_INDICATION, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.NOTES, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.NOTES, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.SPECIES, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.SPECIES, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.E_LANDOWNER, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.E_LANDOWNER, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.EXEM_YEAR, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.EXEM_YEAR, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top">
                        @Html.EditorFor(model => kill.NO_DATA, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => kill.NO_DATA, "", new { @class = "text-danger" })
                    </td>
                    <td valign="top" align="center">
                        <input type="submit" value="Save"  class="btn btn-default" />
                    </td>
                </tr>
            }
        </tbody>
    </table>

}

型号:

    [Table("WHE.ExempKillData")]
    public partial class ExempKillData
    {
        [Display(Name ="Exemption Number")]
        public int? ExemptionNumber { get; set; }

        [Display(Name = "Issue Date")]
        [Column(TypeName = "datetime2")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? IssueDate { get; set; }

        [Display(Name = "Kill Count")]
        public int? KillCount { get; set; }

        public int? TRAPPING { get; set; }

        [Column(TypeName = "numeric")]
        public int? SHOOTING { get; set; }

        public int? DOGS { get; set; }

        [Column(TypeName = "numeric")]
        public int? OTHER { get; set; }

        [Display(Name ="No Indication")]
        public int? NO_INDICATION { get; set; }

        [StringLength(200)]
        public string NOTES { get; set; }

        [StringLength(32)]
        public string SPECIES { get; set; }

        [Display(Name ="Landowner")]
        public double? E_LANDOWNER { get; set; }

        [Display(Name ="Exemption Year")]
        [StringLength(4)]
        public string EXEM_YEAR { get; set; }

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

        public string MethodOfDisposal { get; set; }

        public int ID { get; set; }

    }
}

我希望单击“保存”将使该行的内容返回到“控制器操作”,然后可以在其中正确保存数据。

对于我尝试编辑的每条记录,ModelState.IsValid为true,但是如上所述,除ID和E_Landowner之外的所有字段均为空。

1 个答案:

答案 0 :(得分:1)

您发出请求时是否查看了检查器中的“网络”标签?参数如下所示:

enter image description here

您现在可以在您的环境中进行测试。检查未正确绑定的元素,然后删除“ kill”。从它的名字:

enter image description here

按保存时,此单个属性应正确绑定

问题是,当您创建'EditorFor(model => kill.IssueDate )时,加粗文本将成为您提出请求时的参数名称。因此,如果您的模型具有ExempKillData类型的属性,并命名为'kill',则绑定将起作用。

但是不用担心,我知道您想做什么。

只需更改行

@Html.EditorFor(model => kill.IssueDate, new { htmlAttributes = new { @class = "form-control" } })

@Html.EditorFor(model => kill.IssueDate, null, "IssueDate", new { htmlAttributes = new { @class = "form-control" } });

第二个参数是一个模板,它不是我们在该示例中感兴趣的模板,但是第三个参数告诉ASP.NET为该属性设置一个不同的字段名称。

在解释方面我比较呆板,但我希望你明白这个主意:)

顺便说一句。您可以从方法参数中删除所有这些“绑定”文本。只需输入参数的类型和名称

enter image description here