Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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
Jquery 将json列表传递给MVC3_Jquery_Asp.net Mvc 3 - Fatal编程技术网

Jquery 将json列表传递给MVC3

Jquery 将json列表传递给MVC3,jquery,asp.net-mvc-3,Jquery,Asp.net Mvc 3,我正试图用Ajax将其提交给MVC3控制器: var params = { 'productId': productId, 'list': [] }; $.each($('.specificationId'), function () { var specId = $(this).attr('id').replace('specification_', ''); var specification = {

我正试图用Ajax将其提交给MVC3控制器:

    var params = {
        'productId': productId,
        'list': []
    };

    $.each($('.specificationId'), function () {
        var specId = $(this).attr('id').replace('specification_', '');

        var specification = {
            specificationId: specId,
            articleId: $('#articleid_' + specId).attr('value'),
            price: $('#price_' + specId).attr('value'),
            stock: $('#stock_' + specId).attr('value'),
            stockCount: $('#stockcount_' + specId).attr('value'),
            weight: $('#weight_' + specId).attr('value'),
            visible: $('#visible_' + specId).attr('checked')
        };

        params.list.add(specification);
    });

    console.log(params);
    //all values are parsed fine here, shows an array with json objects

    $.ajax({
        type: "POST",
        url: "/Admin/Product/Save/",
        data: params,
        success: function () { alert('done'); }
    });
现在,这必须转到控制器,如下所示:

    [HttpPost]
    public Boolean Save(Int32 productId, Object[] specifications)
    {

        return true;
    }
但是Object[]不起作用,返回null,我尝试了各种各样的东西,比如模型列表等等,但它将保持null

如何解决此问题?

jQuery
.ajax()
将JSON对象转换为
应用程序/x-www-form-urlencoded
内容类型中的键值对集合。您可以在的博客帖子和评论中阅读对此的详细解释


因此,您应该创建一个自定义ModelBinder,或者将
FormCollection form
作为参数,而不是
Object[]
。将为您提供表单值的
NameValueCollection
,该表单值也实现了
IValueProvider
。您可以在集合中循环,所有数据都将在那里。

尝试将控制器操作更改为(我在这里假设规范是表示您试图发布的JSON的模型):


ASP.NET MVC 3包含内置的JSON模型绑定

因此,创建与您尝试提交的JSON匹配的简单POCO:

public class ProductJsonModel
{
   public int ProductId { get; set; }
   public ProductSpecificationJsonModel[] @List { get; set; }
}

public class ProductSpecificationJsonModel
{
   public int SpecificationId { get; set; }
   public int ArticleId { get; set; }
   public double Price { get; set; }
   public int Stock { get; set; }
   public int StockCount { get; set; }
   public int Weight { get; set; }
   public bool Visible { get; set; }
}
然后在你的行动中接受:

[HttpPost]
public Boolean Save(ProductJsonModel model)
只要JSON对象中的属性名与POCO中的属性名匹配,MVC就会为您绑定


另外-您应该使用
.stringify
或类似的方法序列化JSON,并相应地设置
$.ajax
对象的
contentType

这是我期望的工作,但列表仍然保持为空(控制台输出显示正确的输出)。在进行一些修改之后,添加模型就可以了,但是我不得不删除所有的JSON.stringify引用,这行MVC就是不接受这个列表。事实上,我太高兴了,碰巧我看到数组中的所有行都出现了,但是值没有被填充!现在有点烦了我也有同样的问题。它创建列表项,但每个项的属性值都是默认值(null/0)。我相信这与编码有关,编码率为%5b0%5d。数字=2,编码率为%5b0%5d。金额=876876。你曾经处理过这个问题吗?不可能,因为字段是动态的,而不是静态的。@rogier21,什么是不可能的?无论字段是如何生成的,所有提交后的值都将在FormCollections中。这是我的想法,但测试了几次,我没有得到我自己生成的值。@Rogier21,我很难相信这一点。您可以发布完整的代码和循环通过
FormCollection
中的键的结果吗?很抱歉,这不起作用,它给了我一个错误500:参数字典包含不可为空的类型“System.Int32”的参数“productId”的空项,用于方法“Boolean Save”(Int32,System.Collections.Generic.List`1[型号.规格])'在'Areas.Admin.Controllers.ProductController'中。可选参数必须是引用类型、可为Null的类型,或者声明为可选参数。
参数名称:Parameters我编辑了我的答案,将contentType:'application/json'包括在内。尝试将其添加到AJAX调用中。我认为这会奏效。我看到这是旧的,但对于其他人来说,它是新的可能与以下事实有关:
traditional
标志设置为true时需要传入数组。我昨晚又遇到了这个问题:
public class ProductJsonModel
{
   public int ProductId { get; set; }
   public ProductSpecificationJsonModel[] @List { get; set; }
}

public class ProductSpecificationJsonModel
{
   public int SpecificationId { get; set; }
   public int ArticleId { get; set; }
   public double Price { get; set; }
   public int Stock { get; set; }
   public int StockCount { get; set; }
   public int Weight { get; set; }
   public bool Visible { get; set; }
}
[HttpPost]
public Boolean Save(ProductJsonModel model)