ASP.NET MVC - 使用表单发布多个复杂对象
在 ASP.NET MVC 应用程序中,我尝试使用单个表单提交多个对象。我能够将简单类型发回,但在处理复杂类型时遇到问题。我觉得我模仿了 Phil Haack 在他的博客文章 模型绑定到列表,但没有运气。即使完全复制他的代码也无济于事。
我正在尝试填充一组 MachineLicenseBillback
对象的 ProjectNum
和 TaskNum
属性。不幸的是,IList
在发布时总是以 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" />
<% } %>
In an ASP.NET MVC application I am trying to submit multiple objects with a single form. I am able to get simple types to post back but am having issues with complex types. I feel like I have mimicked the example provided by Phil Haack in his blog post Model Binding To A List but with no luck. Even going so far as copying his code exactly to no avail.
I am trying to populate the ProjectNum
and TaskNum
properties of a set of MachineLicenseBillback
objects. Unfortunately the IList<MachineLicenseBillback> machinePts
always ends up as a null when posted.
What am I missing?
Class
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; }
}
Action
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TrueUp(int id, IList<MachineLicenseBillback> machinePts)
{
// ...
}
Form
<% 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" />
<% } %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MVC 1 RTM 中删除了 .Index 语法,并在 MVC 2 中重新引入。对于 MVC 1,列表元素必须按顺序编号:machinePts[0]、machinePts[1] 等。
The .Index syntax was removed for MVC 1 RTM and reintroduced in MVC 2. For MVC 1, list elements must be numbered sequentially: machinePts[0], machinePts[1], etc.
Scott Hanselman 在此处提供了绑定列表的完整演练项目。简而言之,您的控制器方法需要一个
MachineLicenseBillback
数组,而不是一个IList
。查看您的代码,如果您想绑定到 IDictionary(而不是 IList),您可以在视图中使用键/值对。或者可以保留视图中当前的代码,并使用数组作为控制器方法中的参数。
要特别注意命名约定。如果命名不匹配,模型绑定器将无法获取数据。
Scott Hanselman has a complete walkthrough here for binding lists of items. In short, your controller method needs an array of
MachineLicenseBillback
, not anIList
.Looking at your code, if you want to bind to an
IDictionary
(not an IList), you can use key/value pairs in the view instead. Or can keep the code you currently have in the view, and use an Array as the parameter in the controller method.Pay special attention to the naming conventions. If there is a mismatch in naming, the model binder won't pick up the data.