javascript - c# html array of form elements -
@using (html.beginform()) { @html.validationsummary(true) <div id="elem"> <div class="fields"> <select name="items[1][0]" id="items[1][0]"> <option value="1">book</option> <option value="2">brush</option> </select> <input type="text" value="1" name="items[1][1]" id="items[1][1]"> <a id="test" href="#">remove item</a> </div> </div> </div><a href="#" id="addelem">add item</a></div> <br /> <input type="submit" name="name" value="submit" /> }
basically, has set of 2 fields name field , serial field defined in 2 dimensional array. add item
link works in way when user clicks on, adds new set of fields id being current time var new_id = new date().gettime();
using javascript. example, new set of fields be:
<div class="fields"> <select name="items[1368034980308][0]" id="items[1368034980308][0]"> <option value="1">book</option> <option value="2">brush</option> </select> <input type="text" name="items[1368034980308][1]" id="softwareperasset[1368034980308][1]"> </div>
i submit them edit action edit action go through items
array , add 2 fields.
my edit action looks this:
[httppost] public actionresult edit(formcollection col, string[,] items) { return view(); }
i have set break point on action items
array empty , can access fields through formcollection col
variable.
is there missing items
array not set properly.
json rescue. redesigned form using idea blog (http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx) section non-sequential indices adding them using using timestamp:
<div class="fields"> <input type="hidden" name="items.index" value="1" /> <select name="items[1].name" id="items[1].name"> <option value="1">visio</option> <option value="2">painter</option> </select> <input type="text" name="items[1].serial" id="items[1].serial" /> <a id="test" href="#">remove item</a> </div>
in controller, serialize formcollection variable json easier manipulate:
[httppost] public actionresult edit(formcollection col) { string json = jsonhelper.tojson(col); return view(); }
an example of json
string this:
{ "btnsubmit" : "submit", "items.index" : "1,2,1368212161686,1368212162374,1368212162966,1368212163750", "items[1368212161686].name" : "1", "items[1368212161686].serial" : "", "items[1368212162374].name" : "2", "items[1368212162374].serial" : "", "items[1368212162966].name" : "1", "items[1368212162966].serial" : "98765", "items[1368212163750].name" : "2", "items[1368212163750].serial" : "43210", "items[1].name" : "1", "items[1].serial" : "12345", "items[2].name" : "2", "items[2].serial" : "67890" }
i had create tojson extension able retrieve values or else names being retrieved
public static class jsonhelper { public static string tojson(this system.web.mvc.formcollection collection) { var list = new dictionary<string, string>(); foreach (string key in collection.keys) { list.add(key, collection[key]); } return new system.web.script.serialization.javascriptserializer().serialize(list); } }
Comments
Post a Comment