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。
完美,谢谢。 – Myster 2009-09-29 22:37:50