How to get the data to Post to Asp.net MVC controller action
How to get the data to Post to Asp.net MVC controller action
I am using 'datatable' (based on 'HTML (DOM) sourced data' example) for the first time in my asp.net application
Now i want to convert my table to my Model(or JSON) to post it to the server when the user clicks on the 'Post data' button. this is my controller action:
[HttpPost, ActionName("Index")]
public ActionResult Index(IEnumerable<InslagModel> listInslagen)
{
//save the data hrer
return View();
}
I tried this:
var table = $('#datatable').DataTable();
var data = table
.rows()
.data();
But now i am stuck here, and not sure if this is right...how to proceed from here to get my data Model?
Thanks in advance.
Answers
You would convert it to an array with
toArray()
, then send in a standard JS Ajax call to the server.If you want the client in sync with the server, it would be worth considering Editor, as that's its purpose.
Cheers,
Colin
Thank you for the answer Colin,
I tried:
var table = $('#datatable').DataTable();
It returns only 28 records, while i have 930 records in my table. why is that so and how to get all records?
Are you using
serverSide
? It would also be worth printingplainArray
in aconsole.log()
statement to see how many there are on the client before sending.Colin
Hi Colin,
No, i test and debug it on the client-side. You can check it out here
I changed the code to check the data of rows:
$(document).ready(function () {
var table = $('#datatable').DataTable();
$("#btnPostData").click(function () {
var data = table.rows().data();
alert(JSON.stringify(data));
});
});
It returns only the first 6 records?
I enabled a breakpoint and see that
data
has 930 rows, see screenshot:Are you looking at the Alert message for this?
"7":["324552","<textarea rows=\"2\" cols=\"50\" id=\"barcodes\"></textarea>","<textarea rows=\"2\" cols=\"50\" id=\"remarks\"></textarea>","<input type=\"checkbox\" id=\"IsFullyScanned\" ...
Note the
...
at the end indicating there is more data then the alert will display.Kevin
Hi Kthorngren,
Thank you, I am a beginner in this and trying to experiment and learn, now i see the rest of the records
However, i miss something else too. I entered some text in one of the textareas and clicked on 'Post data', but i am getting a empty data record(without my text) :
"324545"
"<textarea rows="2" cols="50" id="barcodes"></textarea>"
"<textarea rows="2" cols="50" id="remarks"></textarea>"
"<input type="checkbox" id="IsFullyScanned" name="isFullyScanned">"
I was expecting this:
<textarea rows="2" cols="50" id="barcodes">test</textarea>