Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to fill a c# list using JavaScript array in mvc razor .net

here is my code

function SendData() {
        document.getElementById('info').innerHTML = "";
        var myTab = document.getElementById('TB1');
        var Details = [[]];
        // LOOP THROUGH EACH ROW OF THE TABLE AFTER HEADER.

        for (i = 1; i < myTab.rows.length; i++) {

            Details.push([myTab.rows[i].cells[1].children[0].value, myTab.rows[i].cells[2].children[0].value, myTab.rows[i].cells[3].children[0].value]);
            
             @Model.DTList = Details;
        };


this line is not working @
@Model.DTList = Details;


What I have tried:

function SendData() {
        document.getElementById('info').innerHTML = "";
        var myTab = document.getElementById('TB1');
        var Details = [[]];
        // LOOP THROUGH EACH ROW OF THE TABLE AFTER HEADER.

        for (i = 1; i < myTab.rows.length; i++) {

            Details.push([myTab.rows[i].cells[1].children[0].value, myTab.rows[i].cells[2].children[0].value, myTab.rows[i].cells[3].children[0].value]);
            
             @Model.DTList = Details;
        };
Posted
Updated 2-Jun-20 4:15am
Comments
F-ES Sitecore 2-Jun-20 10:13am    
You can't do that because your server code runs first (everything in an @ block) to generate html (including your SendData function) which is sent to the browser to execute, so when the "@Model.DTList = " line is running on your server there is no such thing as "Details". If you want to send data to the server you have to do it as a form postback or using ajax. Reading between the lines, if you are using DTList later on in your view you may have to restructure how you are doing things.
Asif 7969814 2-Jun-20 10:28am    
in ajax method can i send multiple details array
my problem is i want to send more then one list details1, details2, details3,
but in ajax i have to combine all in one (data: JSON.stringify(DetailsAll),)
$.ajax({
type: "POST",
//traditional: true,
url: "/Emp/MasterDetails",
data: JSON.stringify(Details),
contentType: "application/json; charset=utf-8",
dataType: "json",
traditional: true,
success: function (data) {
alert(data.massge + " record(s) inserted.");
}
});
if you have time give me example link of Postback method.

1 solution

That's not how it works. The C# and Razor code executes on the server, and generates a response which is sent back to the client. (In this case, the response is an HTML page containing some Javascript.) The client then executes the Javascript in response to some event on the client, by which time your Model is long gone.

If you want to pass the details from Javascript back to the server, you need to make another request. You can either make an AJAX request, or submit a <form>. The server can then process the data that's send with that request, and return a new response.

Ajax - Developer guides | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900