【发布时间】:2010-01-26 08:23:19
【问题描述】:
在 ASP.NET MVC 应用程序中,我尝试使用单个表单提交多个对象。我能够将简单类型发回,但遇到复杂类型的问题。我觉得我模仿了 Phil Haack 在他的博客文章Model Binding To A List 中提供的示例,但没有运气。甚至完全复制他的代码也无济于事。
我正在尝试填充一组MachineLicenseBillback 对象的ProjectNum 和TaskNum 属性。不幸的是,IList<MachineLicenseBillback> machinePts 在发布时总是以 null 结束。
我错过了什么?
类
public class MachineLicenseBillback
{
public MachineLicenseBillback() { }
public virtual int MachineId { get; set; }
public virtual string ProjectNum { get; set; }
public virtual string TaskNum { get; set; }
public virtual string VerifiedFlag { get; set; }
public virtual DateTime? RcdChgDateTime { get; set; }
public virtual string RcdChgAgent { get; set; }
}
动作
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TrueUp(int id, IList<MachineLicenseBillback> machinePts)
{
// ...
}
表格
<% using (Html.BeginForm("TrueUp", "Home", new { id = Model.Customer.Id },
FormMethod.Post))
{ %>
<input type="hidden" name="machinePts.Index" value="<%= machine.MachineId %>" />
<input type="text" name="machinePts[<%= machine.MachineId%>].ProjectNum"
value="<%= machine.MachineLicenseBillback.ProjectNum %>" />
<input type="text" name="machinePts[<%= machine.MachineId %>].TaskNum"
value="<%= machine.MachineLicenseBillback.TaskNum %>" />
<input type="submit" value="Submit" />
<% } %>
【问题讨论】:
-
我也没有成功让 Phil Haack 的示例代码工作。我还尝试了其他一些绑定到列表的示例,但没有任何运气。