Skip to content Skip to sidebar Skip to footer

Passing List And String Data From Ajax To Controller In Mvc 4, Getting Null For List?

Hi I'm new to JavaScript and MVC, and I'm trying to pass List and string to my controller. JavaScript looks like: var parms = { globalList: globalList,

Solution 1:

It wont work. If you debug a little then you will see that you post to server something like this:

globalList:[objectObject]
globalList:[objectObject]
globalList:[objectObject]

It is just array of strings. But there is a way to do what you want. You can serialize your array to json and then deserialize it in your controller. Just change your param to:

var parms = {
    globalList: JSON.stringify(globalList),
    fieldName: fieldName
};

And action:

[HttpPost]
publicvoidSaveField(string globalList, string fieldName)
{
    var serializer = new JavaScriptSerializer(); //use any serializer you wantvar list = serializer.Deserialize<List<Point>>(globalList);
}

Or you can make param object looks like from HTML form:

var parms = {
     "globalList[0].GeoDuzina": 51.506760996586294, 
     "globalList[0].GeoSirina": -0.06106463202740998,
     "globalList[1].GeoDuzina": 51.516269286402846, 
     "globalList[1].GeoSirina": -0.056258113472722464,
     "globalList[2].GeoDuzina": 51.50419662363912, 
     "globalList[2].GeoSirina": -0.044413478462956846,
     fieldName: fieldName
 };

It works with your action.

There are many other ways to do that. I just show two of them. Is any good for you?

btw. This numbers are too long for floats.;)

Post a Comment for "Passing List And String Data From Ajax To Controller In Mvc 4, Getting Null For List?"