admin管理员组

文章数量:1305181

I have converted a html table content in a array but unable to send it to the controller. Pls help

  • I have tried a lot of references but every time the parameter in controller getting null values
function sendtabledata() {
var element = document.getElementById("pveloandata");
var rows = element.querySelectorAll("tr");


var jsarray = [];

            rows.forEach((r, i) => {
                if (i > 0) {
                    const tabledata = {}
                    const rowitems = r.children
    
                    tabledata["srl"] = i;
                    tabledata["date"] = rowitems[1].textContent;
                    tabledata["balance"] = rowitems[2].textContent;
                    tabledata["intbal"] = rowitems[3].textContent;
    
                    jsarray.push(tabledata);
                }
            })
        
        jsarray = JSON.stringify({ 'jsarray': jsarray });
    
            $.ajax({
                
                ContentType: 'application/json; charset=utf-8',
                dataType: "json",
                type: "POST",
                traditional:true,
                url: '@Url.Action("gettabledata", "Loan")',
                data: jsarray,
               
                success: function (data) {
                   
    
                },
                error: function (err) {
                    alert(err);
                }
            });
    
        }

//controller Method

 public ActionResult gettabledata(string[] jsarray)
        {

           
            return View();
        }

I have converted a html table content in a array but unable to send it to the controller. Pls help

  • I have tried a lot of references but every time the parameter in controller getting null values
function sendtabledata() {
var element = document.getElementById("pveloandata");
var rows = element.querySelectorAll("tr");


var jsarray = [];

            rows.forEach((r, i) => {
                if (i > 0) {
                    const tabledata = {}
                    const rowitems = r.children
    
                    tabledata["srl"] = i;
                    tabledata["date"] = rowitems[1].textContent;
                    tabledata["balance"] = rowitems[2].textContent;
                    tabledata["intbal"] = rowitems[3].textContent;
    
                    jsarray.push(tabledata);
                }
            })
        
        jsarray = JSON.stringify({ 'jsarray': jsarray });
    
            $.ajax({
                
                ContentType: 'application/json; charset=utf-8',
                dataType: "json",
                type: "POST",
                traditional:true,
                url: '@Url.Action("gettabledata", "Loan")',
                data: jsarray,
               
                success: function (data) {
                   
    
                },
                error: function (err) {
                    alert(err);
                }
            });
    
        }

//controller Method

 public ActionResult gettabledata(string[] jsarray)
        {

           
            return View();
        }
Share Improve this question asked Feb 4 at 11:13 Abhisek RoyAbhisek Roy 32 bronze badges 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Feb 4 at 15:27
Add a comment  | 

1 Answer 1

Reset to default 0

I can see issue with your post method below

jsarray = JSON.stringify({ 'jsarray': jsarray });

here you are sending

{"jsarray":[...]} instead of [...]

but you are accepting jsarray[] from server side

you need to update your from front end side like

jsarray = JSON.stringify(jsarray); //this will send array of element

on server side you can use like

public ActionResult gettabledata([FromBody]string[] jsarray)

本文标签: javascripthow to convert html table content to an array and send it to controllerStack Overflow