MVC 3从JSon获取列表

时间:2012-08-23 14:09:17

标签: jquery json asp.net-mvc-3

我通过JSON将表单值发送到控制器。我得到了控制器的所有值。但是当我发送一个列表时,列表项总是为空。我不知道问题是什么。 这是我的观点模型:

public class PromotionLineViewModel
{
    public string PromotionID { get; set; }
    public ConditionList[] ConditionList { get; set; }
    public string ItemUsageType { get; set; }
    public string PriceCalculationType { get; set; }
    public string PromotionDiscount { get; set; }
    public string LimitCheck { get; set; }
    public string MinQuantity { get; set; }
    public string MinAmount { get; set; }
    public string MaxQuantity { get; set; }
    public string MaxAmount { get; set; }
    public string IsActionActive { get; set; }
    public string ActionQuantity { get; set; }
    public string ActionFixed { get; set; }
    public string BundleGroupNr { get; set; }
    public string PalletQuantity { get; set; }
    public string ProductUsageMultiplier { get; set; }
    public string MaxCapAmount { get; set; }
}

[Serializable]
public class ConditionList
{
    public string check { get; set; }
    public string isExclude { get; set; }
    public string value { get; set; }
}

这是我的jquery代码:

function dataPost(url) {
var formData = form2object('prm-form', '.', true);
$.ajax({
    type: 'POST',
    url: url,
    data: formData,
    contentType: "application/json; charset=utf-8",
    traditional: true,
    success: function (data) {
        alert("done");
    }
});

//$.post(url, formData,"json");
//   document.getElementById('testArea').innerHTML = JSON.stringify(formData, null, '\t');  
//}

这就是我得到的:

Controller Image

我已经检查了json值。他们都是正确的。我需要在控制器上获取列表项值。 这是我的json格式:

{
"PromotionID": "000004",
"ConditionList": [
    {
        "check": "12",
        "isExclude": "0",
        "value": "2334"
    },
    {
        "check": "13",
        "isExclude": "1"
    }
],
"ItemUsageType": "1",
"PriceCalculationType": "1",
"PromotionDiscount": "234",
"LimitCheck": "0",
"MinQuantity": "2",
"MinAmount": "2",
"MaxQuantity": "2",
"MaxAmount": "2",
"IsActionActive": "action",
"ActionQuantity": "2",
"ActionFixed": "fix",
"BundleGroupNr": "2",
"PalletQuantity": "2",
"ProductUsageMultiplier": "2",
"MaxCapAmount": "2"
}

控制器操作:

[HttpPost]
    public ActionResult JsonResult(PromotionLineViewModel promotionLineViewModel)
    {
        ...
    }

2 个答案:

答案 0 :(得分:0)

尝试使用:

public List<ConditionList> ConditionList { get; set; } 

而不是

public ConditionList[] ConditionList { get; set; }

如下图所示:

public class PromotionLineViewModel
{
    public string PromotionID { get; set; }
    public List<ConditionList> ConditionList { get; set; }
    public string ItemUsageType { get; set; }
    public string PriceCalculationType { get; set; }
    public string PromotionDiscount { get; set; }
    public string LimitCheck { get; set; }
    public string MinQuantity { get; set; }
    public string MinAmount { get; set; }
    public string MaxQuantity { get; set; }
    public string MaxAmount { get; set; }
    public string IsActionActive { get; set; }
    public string ActionQuantity { get; set; }
    public string ActionFixed { get; set; }
    public string BundleGroupNr { get; set; }
    public string PalletQuantity { get; set; }
    public string ProductUsageMultiplier { get; set; }
    public string MaxCapAmount { get; set; }
}

答案 1 :(得分:0)

直接尝试使用.serialize()方法。因此,如果我们假设您的表单中包含id="prm-form"

function dataPost(url) {
    $.ajax({
        type: 'POST',
        url: url,
        data: $('#prm-form').serialize(),
        success: function (data) {
            alert("done");
        }
    });
}

在这种情况下,您不需要发送JSON。它比那更容易。