asp.net mvc - Binding Numbered Arrays in MVC -


i'm seeing odd (to me) issue attempting post array of integers based on checkboxes. when values not posted in order, model binder doesn't seem function in way expect.

i can reproduce against simple action

    public actionresult debug(string[] unassigned)     {         return redirecttoaction("index", new { id = 7 });     } 

an example of not working when following values posted (copied in via immediate window). expect unassigned have values 8 , 6.

? request.form.allkeys {string[4]} [0]: "__requestverificationtoken" [1]: "loginid" [2]: "unassigned[1]" [3]: "unassigned[3]" ? request.form["unassigned[1]"] "8" ? request.form["unassigned[3]"] "6" ? unassigned null 

when values passed in order, works (note unassigned[3] doesn't bound unassigned[2] not posted.

? request.form.allkeys {string[5]} [0]: "__requestverificationtoken" [1]: "loginid" [2]: "unassigned[0]" [3]: "unassigned[1]" [4]: "unassigned[3]" ? request.form["unassigned[0]"] "2" ? request.form["unassigned[1]"] "8" ? request.form["unassigned[3]"] "6" ? unassigned {string[2]} [0]: "2" [1]: "8" 

minus formatting, html looks this

<input type="checkbox" name="unassigned[0]" value="2"> <input type="checkbox" name="unassigned[1]" value="8"> <input type="checkbox" name="unassigned[2]" value="7"> <input type="checkbox" name="unassigned[3]" value="6"> <input type="checkbox" name="unassigned[4]" value="5"> <input type="checkbox" name="unassigned[5]" value="9"> <input type="checkbox" name="unassigned[6]" value="4"> <input type="checkbox" name="unassigned[7]" value="3"> <input type="checkbox" name="unassigned[8]" value="1"> 

given select not broken, doing wrong?

given select not broken, doing wrong?

you not respecting convention binding list because have holes in indexes. use non-sequential indexes, guids. @ phil haacks blog post have linked to. has entire section dedicated it.

other possibility recommend use view model. go ahead , write one:

public class itemviewmodel {     public string id { get; set; }     public bool selected { get; set; } } 

and have controller:

public class homecontroller : controller {     public actionresult index()     {         var model = new[]          {             new itemviewmodel { id = "2" },             new itemviewmodel { id = "8" },             new itemviewmodel { id = "7" },             new itemviewmodel { id = "6" },             new itemviewmodel { id = "5" },             new itemviewmodel { id = "4" },             new itemviewmodel { id = "3" },             new itemviewmodel { id = "1" },         };         return view(model);     }      [httppost]     public actionresult index(itemviewmodel[] model)     {         // correctly bound here     } } 

and corresponding typed view:

@model itemviewmodel[] @using (html.beginform()) {     (var = 0; < model.length; i++)     {         html.hiddenfor(x => x[i].id)         html.checkboxfor(x => x[i].selected)     }      <button type="submit">ok</button> } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -