我使用Steve Sanderson的BeginCollectionItem方法來添加動態內容。當我在第一級進行時,一切正常。但是,當試圖在另一個BeginCollectionItem中實現嵌套的集合時,它看起來不起作用。嵌套的BeginCollectionItem
我的型號如下:
public class Order
{
[Key]
[HiddenInput]
public int id { get; set; }
[Display(Name = "Order number")]
public string number { get; set; }
...
[Display(Name = "Payment method")]
public List<PaymentMethod> payment_methods { get; set; }
...
}
public class PaymentMethod
{
public MethodOfPayment method { get; set; }
public CC cc { get; set; }
public CASH cash { get; set; }
public TT tt { get; set; }
}
public class TT
{
[Key]
public int id { get; set; }
[Required(ErrorMessage = "{0} is required.")]
[Display(Name = "Total amount")]
public double? total_value { get; set; }
...
[Display(Name = "Transfers")]
public List<Transfer> transfers { get; set; }
}
public class Transfer
{
[Key]
public int id { get; set; }
[Display(Name = "Payment")]
public int payment_id { get; set; }
[Required(ErrorMessage = "{0} is required.")]
[Display(Name = "SWIFT")]
public string swift { get; set; }
[Required(ErrorMessage = "{0} is required.")]
[Display(Name = "Amount transferred")]
public double? transfer_amount { get; set; }
[Required(ErrorMessage = "{0} is required.")]
[Display(Name = "Date transferred")]
public DateTime transfer_date { get; set; }
...
}
現在我已經是一個訂單可能有幾種付款方式,而如果支付方法之一是TT(電傳傳輸)可能有幾個轉讓參與其中。 作爲一個集合實現一個訂單中的多種付款方式,但是當我嘗試在TT內部實施多次轉賬時,這些轉賬沒有任何內容被傳遞給控制器。
這是我的看法是什麼樣子:
@model prj.Models.Model.Order
@using (Html.BeginForm("Create")){
@Html.ValidationSummary(true, "Creation was unsuccessful. Please correct the errors and try again.")
...
@Html.TextBoxFor(m => m.number, new { id = "txtnumber" })
...
<div id="editorPaymentRows">
@foreach (var payment in Model.payment_methods)
{
@Html.Partial("_NewPayment", payment)
}
</div>
}
在_NewPayment部分:
@using prj.Helpers
@model prj.Models.Model.PaymentMethod
<div class="editPaymentRow">
@using (Html.BeginCollectionItem("payment_methods"))
{
...
<div class="editor-label">
@Html.LabelFor(m => m.tt.total_value)<req>*</req>
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.tt.total_value)
</div>
...
<div id="editorTransferRows">
@if (Model.tt != null)
{
foreach (var transfer in Model.tt.transfers)
{
@Html.Partial("_NewTransfer", transfer)
}
}
...
</div>
}
</div>
終於在_NewTransfer部分:
@using prj.Helpers
@model prj.Models.Model.Transfer
...
<div class="editTransferRow">
//using (Html.BeginCollectionItem("transfers"))
@using (Html.BeginCollectionItem("tt.transfers"))
{
...
<div class="editor-label">
@Html.LabelFor(m => m.swift)<req>*</req>
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.swift, new { @class = "t_swift" })
</div>
...
<div class="editor-label">
@Html.LabelFor(m => m.transfer_amount)<req>*</req>
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.transfer_amount, new { @class = "t_transfer_amount" })
</div>
...
}
</div>
所以一切正常,除了在PaymentMethods的TT屬性中的List轉移控制器始終爲空。它沒有正確傳遞給控制器。有什麼我失蹤?
嵌套的BeginCollectionItem不起作用嗎?我還有額外的一步嗎? 請說明一下。 感謝
我想通了使用在以下鏈接所示喬史蒂文斯的方法:
http://www.joe-stevens.com/2011/06/06/editing-and-binding-nested-lists-with-asp-net-mvc-2/
乾杯
你能在請發表您的解決方案? – 2016-05-08 20:32:35