Click here to Skip to main content
14,384,168 members
Rate this:
Please Sign up or sign in to vote.
I am having an data which is string of array, I am trying to pass the data into MVC controller. But i am unable to get the data, instead data I am getting object array null.

What I have tried:

var exportDataToCsv = function () {

    var array = [];
    var headers = [];
    $('#ButtlistTable th').each(function (index, item) {
        headers[index] = $(item).html();
    });
    $('#SearchButtlistTable tr').has('td').each(function () {
        var arrayItem = {};
        $('td', $(this)).each(function (index, item) {
                 arrayItem[headers[index]] = $(item).html();
        });
        array.push(arrayItem);
    });
    //var ExportedData = JSON.stringify(array);


    $.ajax({
        type: "POST",
        url: "/Buttlists/ExportDataToCsv",
        data: JSON.stringify({'ExportedData': array }),
        contentType: 'application/json; charset=utf-8',
        dataType: "json",
        success: function (data) {

            console.log(data);
        }
    });





}


Controller:

public ActionResult ExportDataToCsv(string[] ExportedData)
      {
          var array = ExportedData;
      }
Posted
Updated 11-Oct-19 8:22am
v2
Comments
Afzaal Ahmad Zeeshan 10-Oct-19 2:08am
   
How are you mapping the routes here?
Member 9956700 10-Oct-19 3:34am
   
I am using default routing.

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Kornfeld Eliyahu Peter 10-Oct-19 10:54am
   
Maybe you do not send a string array, but an object array?
Member 9956700 11-Oct-19 0:30am
   
I tried with both, But in Controller i am always getting null of array or objects.
Example: I am 3 values. a[0] = "Apple a[1] = Fruit and a[2] = USA.. in action method i am getting always a[0] = null a[1] = null a[2] = null.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Your Javascript code is not passing an array of strings. It's passing an array of objects, each containing multiple properties.

For example, instead of sending:
["One", "Two", "Three"]
you're sending:
[
    {
        "Column1": "One",
        "Column2": "Two"
    },
    {
        "Column1": "Three",
        "Column2": "Four"
    },
    {
        "Column1": "Five",
        "Column2": "Six"
    }
]
There is no way for ASP.NET to map the data you're sending to an array of strings.

Either change your Javascript code to send the array that your action is expecting; or change your action to accept the data that your Javascript code is sending.
   
Comments
Member 9956700 13-Oct-19 23:59pm
   
Can you advise Me what changes needs to be done in action method to receive above mentioned data.
Richard Deeming 14-Oct-19 6:59am
   
Not really - I don't know what your source data is, or what your action is doing with it.

NB: If you're just trying to export a table to CSV, then you don't need to send the data to the server; you can do it directly from Javascript:
Use JavaScript to Export Your Data as CSV - Chris Grimes[^]
Member 9956700 14-Oct-19 23:41pm
   
Thanks Richard for your support. But there is a small twist- Yes I 'm trying to export table to CSV but I need to open/show the "save dialog box" by which I can give my preferred location and name to the file.
Is there any way I can proceed?
Richard Deeming 15-Oct-19 7:18am
   
You can suggest the file name, but you can't control the location, regardless of whether you generate the file from Javascript or from the server.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100