asp.net core - MVC controller not accepting multidimensional JSON -
i using asp.net 5 rc1 update1
i have jquery ajax call posts json data of html table via post mvc controller method. controller method can't seem receive data.
html:
<table id="datatab" border="1"> <tbody> <tr role="row" class="odd"> <td class="sorting_1" style="display: none;">2087971</td> <td>1</td> <td>1</td> <td>aaaa</td> <td>john smith</td> <td><input type="text" name="value1" value="1"></td> <td><input type="text" name="value2" value="2"></td> <td><input type="text" name="value3" value="3"></td> <td></td> </tr> <tr role="row" class="even"> <td class="sorting_1" style="display: none;">2087972</td> <td>2</td> <td>2</td> <td>bbbb</td> <td>peter parker</td> <td><input type="text" name="value1" value="4"></td> <td><input type="text" name="value2" value="4"></td> <td><input type="text" name="value3" value="4"></td> <td></td> </tr> </tbody> </table> <button id="getdata">get data</button>
jquery:
<script> $("#getdata").click(function (event) { debugger; event.preventdefault(); var tabledata = new array(); $('#datatab tbody tr').each(function (row, tr) { tabledata[row] = { "id": $(tr).find('td:eq(0)').text() , "input1value": $(tr).find('td:eq(5) input').val() , "input2value": $(tr).find('td:eq(6) input').val() , "input3value": $(tr).find('td:eq(7) input').val() } }); var mydata = json.stringify(tabledata); $.ajax({ type: "post", contenttype: "application/json; charset=utf-8", url: '@(url.action("updatedata", "mycontroller"))', data: "mydata=" + mydata, success: function (msg) { } }); }); </script>
since first thing want verify controller can receive data change controller method
public iactionresult updatedata(string[][] mydata)
however when put breakpoint on actionresult line , examine mydata's value it's empty array.
i used fiddler confirm if json data sent , fiddler confirm this:
mydata=[{"id":"2087971","input1value":"1","input2value":"2","input3value":"3"},{"id":"2087972","input1value":"4","input2value":"4","input3value":"4"}]
i have tried adding [frombody] actionmethod parameter
public iactionresult updatedata([frombody] string[][] mydata)
but mydata value null
.
whey doesn't mydata
variable populated. doing wrong?
the data want send looks more collection/array of dictionary of string,string.
[{ "id": "2087971", "input1value": "1", "input2value": "2", "input3value": "3" }, { "id": "2087972", "input1value": "4", "input2value": "4", "input3value": "4" }]
so change action method parameter type
[httppost] public iactionresult updatedata([frombody] list<dictionary<string,string>> mydata) { // mydata; return json(mydata); }
also, in ajax call, send stringified version of array data property value.
var mydata = json.stringify(tabledata); $.ajax({ type: "post", contenttype: "application/json; charset=utf-8", url: '@(url.action("updatedata", "mycontroller"))', data: mydata, success: function (msg) { console.log(msg); } });
Comments
Post a Comment