tags:

views:

30

answers:

3

Not really a requirement or anything yet, but can you do this in a controller:

public ActionResult Edit(IEnumerable<Contact> contacts)
{
    //Loop through and save all records
    return View();
}

This comes from wanting to have multiple records on a form, WITH NO GRID and submit all the items. The HTML would be similar to this

<form>
    <input type="text" id="FirstName" value="Joe"/><input type="text" id="LastName" value="Smith"/><input type="hidden" id="PK" value="1"/>
    <input type="text" id="FirstName" value="Joe"/><input type="text" id="LastName" value="Smithen"/><input type="hidden" id="PK" value="2"/>
    <input type="text" id="FirstName" value="Joe"/><input type="text" id="LastName" value="Smiths"/><input type="hidden" id="PK" value="3"/>
    <input type="text" id="FirstName" value="Joe"/><input type="text" id="LastName" value="Smithy"/><input type="hidden" id="PK" value="4"/>
    <input type="text" id="FirstName" value="Joe"/><input type="text" id="LastName" value="Smithers"/><input type="hidden" id="PK" value="5"/>
<input type="submit" value="Save"/>
</form>

Do you have to create your own router or what? Anyone? Bueller?

A: 

You need this:

<form>
    <input type="text" name="contacts[0].FirstName" id="contacts[0].FirstName" value="Joe"/>
    <input type="text" name="contacts[0].LastName" id="contacts[0].LastName" value="Smith"/>
    <input type="hidden" name="contacts[0].PK" id="contacts[0].PK" value="1"/>
    ....
    <input type="text" name="contacts[5].FirstName" id="contacts[5].FirstName" value="Joe"/>
    <input type="text" name="contacts[5].LastName" id="contacts[5].LastName" value="Smithen"/>
    <input type="hidden" name="contacts[5].PK" id="contacts[5].PK" value="2"/>

    <input type="submit" value="Save"/>
</form>
eu-ge-ne
Good start...the Hanselman link is perfect
KirkHofer
A: 

First of all, replace all "id" with the "name" attribute. A document may not have multiple elements with the same ids.

As to your question, it is not possible out of the box. But it's very easy to parse form value, it should be just a comma separated string.

User
A: 

This post from Scott Hanselman discusses data binding collections in detail:
ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries

Robert Harvey