Herrozerro
Herrozerro

Reputation: 1681

Sending up a list of objects on a create form

I have a view specific model that combines a few of my objects together but I was having a few issues with a list of objects.

My model is so:

public class RouteSubcontract
{
    public RoutingSubcontracts Subcontracts { get; set; }
    public RoutingPhases Phases { get; set; }
    public List<RoutingApprovals> Approvals { get; set; }
}

I have my create form and everything is working correctly, I am using html helpers like so:

@Html.EditorFor(model => model.Subcontracts.subRevNbr, new { htmlAttributes = new { @class = "textReplace", @id = "frmRevNbr" } })

But the problem is when I need to have the list of approvals, I am not even sure how to start with this. I need the ability to create a list of items, I can with jquery have a button that then creates the row of textboxes I need to enter the data, but I am unsure how to ID or name them so that they are picked up correctly by my post back.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AdminRoutingCreate(RouteSubcontract rs)

Upvotes: 0

Views: 13

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239270

For collections the modelbinder expects inputs named in the following format: ListProperty[Index].Property. For example, if you wanted to edit a property on RoutingApprovals named Foo, you're need something like:

<input type="text" name="Approvals[0].Foo" />
<input type="text" name="Approvals[1].Foo" />
<input type="text" name="Approvals[2].Foo" />

On post back you'd end up with three RoutingApprovals instances.

Upvotes: 1

Related Questions