ASP.Net MVC 3 - JSON模式绑定到数组 [英] ASP.Net MVC 3 - JSON Model binding to array

查看:66
本文介绍了ASP.Net MVC 3 - JSON模式绑定到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.Net MVC 3,并通过在支持的功能列表中去,我应该能够得到默认的JSON模型绑定工作开箱。但是我还没有成功地从JSON结合数组/集合的操作方法参数。虽然我没有得到简单的JSON对象绑定工作的权利。将大大AP preciate如果这里的专家能告诉我什么,我做错了。

I am on ASP.Net MVC 3, and going by the feature list supported in at, i should be able to get default json model binding working out of the box. However i havent been successful in binding an array/collection from json to the action method parameter. Although I did get simple json object binding working right. Would greatly appreciate if an expert here could tell me what i am doing wrong.

下面是code:

服务器端code第一:

Server side code first:

//操作方法

 public JsonResult SaveDiscount(IList<Discount> discounts)
    {
       foreach(var discount in discounts)
       {
       ....
       }
    }

//浏览模式

public class Discount
{
    string Sku{get; set;}
    string DiscountValue{get; set;}
    string DiscountType{get; set;}

}

//客户端(jQuery的/ JS):

//client side(jquery/js):

    var discount = {};
    var jsondatacoll = [];
    $('#discountgrid tr').each(function () {

        sku = $(this).find("td").eq(1).html();
        discValue = $(this).find('.discval').val();
        discType = $(this).find('.disctype').val();

        discount = { Sku: sku, DiscountType: discType, DiscountValue: discValue};
        jsondatacoll.push(discount);
        }
    })
    if (jsondatacoll.length > 0) {
        var catalogDiscount = JSON.stringify(jsondatacoll);

        $.ajax(
        {
            url: '/url/savediscount',
            type: 'POST',
            data: catalogDiscount,
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function (data, textStatus, jqXHR) {
                ...                   
            },
            error: function (objAJAXRequest, strError) {                 
               ...
            }
        }
     );   //ajax
    }

我做检查的JSON有效载荷的小提琴手,它看起来象下面这样:

i did check the json payload in fiddler and it look like below:

[
    {"Sku":"sku1","DiscountType":"type1","DiscountValue":"10"},     
    {"Sku":sku2","DiscountType":"type1","DiscountValue":"12"}, 
    {"Sku":"sku3","DiscountType":"type2","DiscountValue":"40"}
]

和服务器端我看到的的IList&LT;折扣&GT; 折扣已经填充了3个空折扣对象 - 这意味着该属性为null,但折扣参数的长度为3

And on the server side i do see the IList<Discount> discounts has been populated with 3 empty Discount objects - meaning the properties are null but the length of the discounts argument is 3.

推荐答案

新鲜中正确地指出这个问题的意见的模特属性必须标明公开。

As Cresnet Fresh rightly pointed out in the comments to the question the model properties must be marked public.

所以修改折扣类,如下解决了这个。

So modifying Discount class as below resolved this.

public class Discount
{
    public string Sku{get; set;}
    public string DiscountValue{get; set;}
    public string DiscountType{get; set;}

}

这篇关于ASP.Net MVC 3 - JSON模式绑定到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆