javascript - Form with Table, submit all values to IEnumerable Controller action -


i have form data listed in table structure

@model ienumerable<mytype> @html.antiforgerytoken() @using (ajax.beginform("save", "noc", null, ajaxopts, new { @enctype = "multipart/form-data", @id = "myform", @name = "myform" })) {  <table> <tr> <td><input type="hidden" name="mytype[0].id" id="mytype[0].id" value="16" /></td> <td><input type="text" name="mytype[0].name" id="mytype[0].name" value="jim" /></td> <td><input type="checkbox" name="mytype[0].selected" id="mytype[0].selected" checked /></td> </tr> <tr> <td><input type="hidden" name="mytype[1].id" id="mytype[1].id" value="17" /></td> <td><input type="text" name="mytype[1].name" id="mytype[1].name" value="bob" /></td> <td><input type="checkbox" name="mytype[1].selected" id="mytype[1].selected" /></td> </tr> </table> <button id="process" value="save" class="btn btn-primary">save</button> } 

in chrome can see in form data section list of data being posted controller....

mytype[0].id: 16 mytype[0].name: jim mytype[0].selected: on mytype[1].id: 17 mytype[1].name: bob 

but in controller null

[httppost] public async task<actionresult> savedata(ienumerable<mytype> model) {   return json("hi"); }  

also if in preview on chrome dev tools see "hi" in preview returned controller.

my javascript looks this

$("#process").on("click", function (event){   event.preventdefault();   var formdata = $("#myform").serialize();   $.ajax({     url: url,     type: "post",     data: formdata   }) }); 

model is

public class mytype {   public int id { get; set; }   public string name { get; set; }   public bool selected { get; set; } } 

there no javascript errors showing, no data controller?

any hugely appreciated.

thanks

your manually generating html have name attributes not relate model. in order post ienumerable<mytype> model elements need be

<input type="hidden" name="[0].id /> <input type="hidden" name="[1].id /> 

etc. should remove id attributes since these invalid , jquery function attempts use them fail (for example . (dot) interpreted class identifier).

note use of checkbox not work since value of on not bind boolean value.

based on comments, generating html using javascript function dynamically add new elements. if so, suggest review this answer. should populate initial model objects , use for loop generate correct html (make model ilist<mytype>)

for(int = 0; < model.count; i++) {   @html.hiddenfor(m => m.id)   @html.textboxfor(m => m.name)   @html.checkboxfor(m => m.selected) } 

and inspect html generates.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -