gpt4 book ai didi

asp.net-mvc-3 - 嵌套的 BeginCollectionItem

转载 作者:行者123 更新时间:2023-12-01 16:08:35 35 4
gpt4 key购买 nike

我正在使用 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 中实现多次转账时,这些转账中的任何一个都不会传递到 Controller 。

这是我的观点:

@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>

所以一切正常,除了在 Controller 中,PaymentMethods 的 TT 属性中的列表传输始终为空。它没有正确传递到 Controller 。我有什么遗漏的吗?

嵌套的 BeginCollectionItem 不起作用吗?我还必须执行额外的步骤吗?请透露一些信息。谢谢

<小时/>

我使用 Joe Stevens 的方法解决了这个问题,如下链接所示:

http://www.joe-stevens.com/2011/06/06/editing-and-binding-nested-lists-with-asp-net-mvc-2/

干杯

最佳答案

要获取带有 Html.BeginCollectionItem 的前缀,您可以访问 ViewData.TemplateInfo.HtmlFieldPrefix (我正在使用 nuget 包)。您使用 tt.transfers 的方向是正确的,但您需要特定的前缀。

而不仅仅是

Html.BeginCollectionItem("tt.transfers")

您还需要当前 payment_method 的前缀。

@{
var paymentMethodPrefix = ViewData.TemplateInfo.HtmlFieldPrefix;
}
@using (Html.BeginCollectionItem(paymentMethodPrefix + ".tt.transfers"))

快速测试看起来您也可以:

@using (Html.BeginCollectionItem(ViewData.TemplateInfo.HtmlFieldPrefix + ".tt.transfers"))

关于asp.net-mvc-3 - 嵌套的 BeginCollectionItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9575732/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com