Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Javascript数组传递给ASP.NET MVC控制器_Javascript_Asp.net Mvc - Fatal编程技术网

将Javascript数组传递给ASP.NET MVC控制器

将Javascript数组传递给ASP.NET MVC控制器,javascript,asp.net-mvc,Javascript,Asp.net Mvc,我尝试将以下javascript类传递给ASP.NET MVC控制器 var postParams = { attributes : [ { name : '', description: '', attributeType : '',

我尝试将以下javascript类传递给ASP.NET MVC控制器

 var postParams = {

         attributes : [
                         {
                            name : '',
                            description: '',
                            attributeType : '',
                            value : ''
                        }
                      ]
    };

 postParams.attributes[0] = new Object();
 postParams.attributes[0].name = "test";
 postParams.attributes[0].description = "test";
 postParams.attributes[0].attributeType = "test";
 postParams.attributes[0].value = "test";
以下是我如何调用控制器方法:

  var actionURL = "@Url.Action("Save", "Catalog")";
  $.ajax({
                    type: "POST",
                    url: actionURL,
                    data:  postParams   ......
在控制器端,我声明了一个ViewModel,如下所示:

 public class AttributeViewModel
 {
    public string name { get; set; }
    public string description { get; set; }
    public string attributeType { get; set; }
    public string value { get; set; } 
 }
 public JsonResult Save(AttributeViewModel[] attributes)
我的控制器保存方法声明如下:

 public class AttributeViewModel
 {
    public string name { get; set; }
    public string description { get; set; }
    public string attributeType { get; set; }
    public string value { get; set; } 
 }
 public JsonResult Save(AttributeViewModel[] attributes)
当我执行时,属性的值总是空的

有什么想法吗?甚至不知道如何开始调试此程序。

您可以尝试使用库来解决您的问题

  [JsonFilter(Param = "attributes", JsonDataType = typeof(AttributeViewModel[]))]
  public JsonResult Save(AttributeViewModel[] attributes)
在客户处:

$.ajax({
        type: 'POST',
        url: url,
        async: true,
        data:  JSON.stringify(attributes), //!!! Here must be the same name as in controller method
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {

        },
        error: function (xhr, ajaxOptions, thrownError) {

        }
    });

似乎需要将
traditional:true
添加到ajax调用中。看一看,然后


谢谢你的建议。添加了对Json.net的引用并添加了过滤器,但null仍然传递给属性。你能帮我把
[JsonFilter(Param=“mydata”,JsonDataType=typeof(AttributeViewModel[])]放在哪里吗?
我得到一个错误:
找不到类型或名称空间名称“jsonfiltertribute”
以传统:true添加,但它没有解决问题。我在这个问题上已经讨论太久了,所以现在我将把一个逗号分隔的数组传递给控制器。