views:

321

answers:

2

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" />
<% } %>
A: 

Scott Hanselman has a complete walkthrough here for binding lists of items. In short, your controller method needs an array of MachineLicenseBillback, not an IList.

public ActionResult TrueUp(int id, MachineLicenseBillback[] machinePts)
{
  // ...
}

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.

Robert Harvey
Tried switching the TrueUp action to look for an array of `MachineLicenseBillback` objects and am having the same issue: machinePts is `null` on post back. I had read Hanselman's post but the `IList` in Haack's example seemed clearer.
ahsteele
A: 

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.

Levi
Well, that's too bad ... do you have a link indicating where this was removed from the MVC 1 RTM? More curious as to the *why*, not that it really matters considering MVC 2 will be out soon enough.
ahsteele
See http://forums.asp.net/p/1377775/2906260.aspx. Briefly, not enough people asked for it, so it was removed and QA resources were moved elsewhere.
Levi