MVC4-提交到主视图期间的部分视图列表模型绑定

时间:2019-05-09 06:45:30

标签: asp.net asp.net-mvc asp.net-mvc-4 binding partial-views

我有一个视图模型,其中有一个子模型列表以呈现部分视图(如下)。

public class PRDocument
{
        [Key]
        [Column(Order = 0)]
        public int Id { get; set; }

        [DisplayName("Vendor Name")]
        [Column(Order = 2)]
        public int VendorId { get; set; }

        public virtual ICollection<PRDocumentQuotation> PRDocumentQuotations { get; set; }

        [NotMapped]
        public List<PRDocumentQuotation> Quotations { get; set; }

        public PRDocument()
        {
            Quotations = new List<PRDocumentQuotation>();
        }
}

    public class PRDocumentQuotation
    {
        [Key]
        [Column(Order = 0)]
        public int Id { get; set; } 

        [Required]
        [Column(Order = 1)]
        public int PRDocumentId { get; set; }

        [Display(Name = "Uploaded File")]
        [Column(Order = 2)]
        public string FileName { get; set; }
    }

并在部分视图中呈现为这样。

@Html.Partial("_PRDocs", Model.Quotations)

这是我的部分观点。

@model IEnumerable<JKLLPOApprovalApp.Models.PRDocumentQuotation>

<table class="table">
   <tr>
       <th>
           @Html.DisplayNameFor(model => model.FileName)
       </th>

       <th></th>
   </tr>

@foreach (var item in Model) {
   <tr>
       @Html.HiddenFor(modelItem => item.PRDocumentId)
       <td>
           @Html.DisplayFor(modelItem => item.FileName)
       </td>     
       <td>
           @Html.ActionLink("Delete", "Delete", new { id=item.Id })
       </td>
   </tr>
}

</table>

以及控制器动作

  [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(PRDocument pRDocument)
        {
            if (ModelState.IsValid)
            {
                pRDocument.PRDocumentQuotations = pRDocument.Quotations;
                db.tbl_PRDocuments.Add(pRDocument);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(pRDocument);
        }

查看数据主视图

@model JKLLPOApprovalApp.Models.PRDocument

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

<h2>Create</h2>

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

    <div class="form-horizontal">
        <h4>PRDocument</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.VendorId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.VendorId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.VendorId, "", new { @class = "text-danger" })
            </div>
        </div>

        @Html.Partial("_PRDocs", Model.Quotations)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>




}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

我想要的是将部分视图数据列表(PRDocumentQuotation)放入与主模型(PRDocument)绑定的Create操作中。我该怎么办?

1 个答案:

答案 0 :(得分:1)

有趣的问题:)

问题在于,Model绑定到列表应该具有唯一的名称。因此,生成的HTML应该如下所示:

<input id="Quotations_0__PRDocumentId" name="Quotations[0].PRDocumentId" type="hidden" value="0">

<input id="Quotations_1__PRDocumentId" name="Quotations[1].PRDocumentId" type="hidden" value="0">

推荐的解决方案是使用编辑器模板,选中thisthis

但是我在下面提供了一个替代解决方案,使用for循环创建带有索引的唯一名称,该名称取自面临相同问题的this帖子。

在主视图中:

改为通过主模型

@Html.Partial("_PRDocs", Model)

部分视图:

@model JKLLPOApprovalApp.Models.PRDocument

<table class="table">
    @if (Model.Quotations != null)
    {
        for (var i = 0; i < Model.Quotations.Count(); i++)
        {
            <tr>
                <th>
                    @Html.DisplayNameFor(model => Model.Quotations[i].FileName)
                </th>

                <th></th>
            </tr>

            <tr>
                @Html.HiddenFor(modelItem => Model.Quotations[i].PRDocumentId)
                <td>
                    @Html.DisplayFor(modelItem => Model.Quotations[i].FileName)
                </td>
                <td>
                    @Html.ActionLink("Delete", "Delete", new { id = Model.Quotations[i].Id })
                </td>
            </tr>
        }
    }

</table>

希望有帮助。