MVC 2 - JavaScript 控制器数组不工作

MVC 2 - JavaScript array to controller is not working

我正在使用 MVC2 和 .net Framework 3.5

在尝试将单个对象传递给 mvc 控制器时,它工作正常。但是当我尝试将值作为数组传递时,我在控制器中得到的 属性 值为 null。

我正在使用以下代码来执行此操作。

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;
            }

    });

控制器代码

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

}

LayerViewModel class

public class LayerViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

在 .net 3.5 中将列表传递给控制器​​的另一种方法是将它们作为 json 字符串传递并从控制器反序列化

 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);
}