0

I am working in MVC2 and .net framework 3.5

While trying to pass a single object to mvc controller it is working fine. But when I try to pass the value as an array, I'm getting property values as null in the controller.

I'm using the below code to do this.

JavaScript

var tdmsConfiguredLayersList = [{Id:1,Name:'Test1'},{Id:2,Name:'Test2'}];
    $.ajax({
            type: "POST",
            //contentType: 'application/json; charset=utf-8',
            //JSON.stringify({ layers: tdmsConfiguredLayersList }),
            data:{layers:tdmsConfiguredLayersList},
            url: rootUrl + "Map/CatalogueDrawing",
            dataType: "json",
            success: function (result) {
                debugger;
            },
            error: function (errResult) {
                debugger;
            }

    });

Controller Code

[HttpPost]
public void CatalogueDrawing(List<LayerViewModel> layers)
{

}

Result that is returned

LayerViewModel class

public class LayerViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}
9
  • 1
    Your commened code was almost there. Your need to add contentType: 'application/json; charset=utf-8', and use data: JSON.stringify({ layers: tdmsConfiguredLayersList }) Commented Jul 22, 2016 at 11:52
  • I have tried that also. But it was not working Commented Jul 22, 2016 at 11:54
  • It does work :) (And assuming its MapController, then use url:'@Url.Action("CatalogueDrawing", "Map")', - don't hard code url's) Commented Jul 22, 2016 at 11:55
  • adding Content type and json.stringify will solve the issue in .net 4.0 and mvc4. Currently we are working in mvc 2 and .Net 3.5. When trying these in 3.5 layers object in controller is null Commented Jul 22, 2016 at 12:02
  • Have a look at this article_ - the paragraph on the JsonValueProviderFactory and also this article. Otherwise you may need to generate the data to use the default application/x-www-form-urlencoded; charset=UTF-8 - i.e. data: { [0].Id: 1, [0].Name: 'Test1', [1].Id: 2, [1].Name: 'Test2' }, Commented Jul 22, 2016 at 12:14

1 Answer 1

0

An alternative way to pass your list to controller in .net 3.5 was pass them as json string and deserialize it from controller

 using System.Web.Script.Serialization;



 var tdmsConfiguredLayersList = [{Id:1,Name:'Test1'},{Id:2,Name:'Test2'}];
  tdmsConfiguredLayersList = JSON.stringify(tdmsConfiguredLayersList);
    $.ajax({
            type: "POST",
            data: { jsonString: tdmsConfiguredLayersList },
            url: rootUrl + "Map/CatalogueDrawing",
            dataType: "json",
            success: function (result) {
                debugger;
            },
            error: function (errResult) {
                debugger;
            }

    });


public void CatalogueDrawing(string jsonString)
{
        var jss = new JavaScriptSerializer();
        List<LayerViewModel> layers = jss.Deserialize<List<LayerViewModel>>(jsonString);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.