2009-09-29 131 views
2

我有一个嵌套列表我希望接受作为我的行动的参数。 我已经使用Phil Haack's Post作为起点,它可以很好地处理单个级别的列表,但是当参数更复杂时,模型联编程序会将空值传递给我的操作。 (我还没有模型绑定的引擎盖下还冒险,做我需要为这个例子吗?)模型绑定到嵌套列表

操作:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Payment(..., List<AddonParticipants> addonParticipants) 
{...} 

型号:

// this participant information will be added to the basket 
// onto the activity which has the matching Guid. 
public class AddonParticipants 
{ 
    public string Guid { get; set; } 
    public List<ParticipantDetails> Participants { get; set; } 
} 
public class ParticipantDetails 
{ 
    [Required(ErrorMessage = "Please enter participant's first name")] 
    public string FirstName { get; set; } 

    [Required(ErrorMessage = "Please enter participant's last name")] 
    public string LastName { get; set; } 
} 

查看伪代码:

foreach (...) 
{ 
    Html.Hidden("addonParticipants.Index", item.Addon.Guid) 
    Html.Hidden("addonParticipants["+item.Addon.Guid+"].Guid", item.Addon.Guid) 
    for (int i = 0; i < item.Addon.SubQuantity; i++) 
    { 
     Html.Hidden("addonParticipants[" + item.Addon.Guid + "].Participants.Index", i) 
     Html.TextBox("addonParticipants[" + item.Addon.Guid + "].Participants[" + i + "].FirstName", item.Addon.Participants[i].FirstName) 
     Html.TextBox("addonParticipants[" + item.Addon.Guid + "].Participants[" + i + "].LastName", item.Addon.Participants[i].LastName) 
    } 
} 

感激的建议。

干杯。

Murray。

回答

2

在RTM你DEET沟的.index隐藏和你的数组索引必须是零索引的整数

for(int j = 0; j < ...) 
{ 
     var item = items[j]; // or what ever 
     Html.Hidden("addonParticipants["+j+"].Guid", item.Addon.Guid) 
     for (int i = 0; i < item.Addon.SubQuantity; i++) 
     { 
       Html.TextBox("addonParticipants[" + j + "].Participants[" + i + "].FirstName", item.Addon.Participants[i].FirstName) 
       Html.TextBox("addonParticipants[" + j + "].Participants[" + i + "].LastName", item.Addon.Participants[i].LastName) 
     } 
} 
+0

完美,谢谢。 – Myster 2009-09-29 22:37:50