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

BekariBekari Posts: 9Questions: 5Answers: 0
edited July 2020 in Free community support

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

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    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

  • BekariBekari Posts: 9Questions: 5Answers: 0

    Thank you for the answer Colin,

    I tried:
    var table = $('#datatable').DataTable();

    var plainArray = table
        .column(0)
        .data()
        .toArray();
    

    It returns only 28 records, while i have 930 records in my table. why is that so and how to get all records?

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Are you using serverSide? It would also be worth printing plainArray in a console.log() statement to see how many there are on the client before sending.

    Colin

  • BekariBekari Posts: 9Questions: 5Answers: 0

    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?

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    edited July 2020

    I enabled a breakpoint and see that data has 930 rows, see screenshot:

    It returns only the first 6 records?

    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

  • BekariBekari Posts: 9Questions: 5Answers: 0

    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>

This discussion has been closed.