asp.net mvc 4 - passing model back to controller with checkboxes comes back empty -


trying grasp how pass following data model controller. want consume model , add database afterwards. when submit form controller, model empty. because every thing else null? have pass else hidden fields? how sort out on view before getting controller?

my controller deserializes xml file looks view

<category>     <id>1</id>     <description>movies</description>     <genre>         <genres>             <id>1</id>             <name>comedy</name>         </genres>         <genres>             <id>2</id>             <name>action</name>         </genres>         <genres>             <id>3</id>             <name>adventure</name>         </genres>         <genres>             <id>4</id>             <name>drama</name>         </genres>         <genres>             <id>5</id>             <name>romance</name>         </genres>     </genres> </category> 

the view / form looks

   <form>    <ul>    @for (int x = 0; x < model.categories[i].genres.count(); x++)    {       <li>            <label for="@model.categories[i].genres[x].name">            <input type="checkbox" name="@model.categories[i].genres[x].name" value="@model.categories[i].genres[x].id" checked="@model.categories[i].genres[x].selected" /> @model.categories[i].genres[x].name            </label>       </li>     }     </ul>     </form> 

give checkboxes fixed name, genreids.

then, in action should receive string[] genreids parameter.

when posting form, genreids posted array, must received in array parameter.

if doesn't work because have many categories , want receive each group of genreids in own category, can send json representation of form values , receive , deserialize on server side. so:

on razor template:

  1. use name of genre name checkboxes in each category

handle form sumit event, and:

  1. use jquery serializearray, put form elements in array
  2. then use json.stringify convert array json format
  3. finally copy string hidden field fixed name , post form. i.e. in hidden field name "serializedformvalues"

on server side:

  1. add parameter action name "serializedformvalues", , type = string
  2. get value of parameter , deserialize received json string, , use on server side

if use json.net can convert json xml, or to anonymous type object. there posibilities.

remember in case, genre ids string[] (or int, if ti's case) , arrays contain checked values.

there last posibility processing request.form "manually". harder do.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -