0

我有一个使用 BeginCollectionItem (CollectionEditingHtmlExtension) 的部分视图绑定。我可以毫无问题地创建列表,但是在发布操作时返回发布的模型数据集合 (public List PerchaceOrderItems { get; set; }) 返回空。任何帮助,将不胜感激。

我的模型,

public class Request
{
    public int Id { get; set; } 
    public string Type { get; set; }
    public virtual PerchaceOrder PerchaceOrder { get; set; }

    public Request()
    {
        PerchaceOrder = new PerchaceOrder();
    }
}

public class PerchaceOrder
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<PerchaceOrderItem> PerchaceOrderItems { get; set; }
    public PerchaceOrder()
    {
        PerchaceOrderItems = new List<PerchaceOrderItem>();
    }
}

public class PerchaceOrderItem
{
    public int Id { get; set; }
    public string ItemName { get; set; }
}

这些是正在使用的视图,

  1. 请求.chtml

主请求视图,用于将模型数据传递给部分视图。

@model InventoryManagementSystem.Models.Modules.Request 

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

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

@Html.Partial("_PerchaceOrder", Model) 

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Create" class="btn btn-default" />
    </div>
</div>
}
  1. _PerchaceOrder 部分视图

    @model InventoryManagementSystem.Models.Modules.Request
    
<hr />

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

<fieldset>
    <legend>Perchase Order Items</legend>

    @if (Model.PerchaceOrder.PerchaceOrderItems == null || Model.PerchaceOrder.PerchaceOrderItems.Count == 0)
    {
        <p>None.</p>
    }

    <ul id="movieEditor" style="list-style-type: none">

        @if (Model.PerchaceOrder.PerchaceOrderItems != null)
        {
            foreach (InventoryManagementSystem.Models.Modules.PerchaceOrderItem Item in Model.PerchaceOrder.PerchaceOrderItems)
            {
                Html.RenderPartial("_PerchaceOrderItem", Item);
            }
        }

    </ul>

    <a id="addAnother" href="#">Add another</a>

    <script type="text/javascript">
        $(function () {
            $("#addAnother").click(function () {
                $.get('/Test/MovieEntryRow', function (template) {
                    $("#movieEditor").append(template);
                });
            });
        });
    </script>

</fieldset>
  1. _PerchaceOrderItem

        @model InventoryManagementSystem.Models.Modules.PerchaceOrderItem
        @using InventoryManagementSystem.Infrastructure;
    
       <li style="padding-bottom:15px">
       @using (Html.BeginCollectionItem("PerchaceOrderItems"))
       { 
     <div class="row">
         <div class="col-md-4">
             <div class="form-group">
                 @Html.LabelFor(model => model.ItemName, htmlAttributes: new { @class = "control-label col-md-2" })
                 <div class="col-md-10">
                     @Html.EditorFor(model => model.ItemName, new { htmlAttributes = new { @class = "form-control" } })
                     @Html.ValidationMessageFor(model => model.ItemName, "", new { @class = "text-danger" })
                 </div>
             </div>
    
         </div> 
    
     </div>
    
     <a href="#" onclick="$(this).parent().remove();">Delete</a>
    

    }

4

0 回答 0