0

我通过 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');  
//}

这就是我得到的:

控制器图像

我已经检查了 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)
    {
        ...
    }
4

2 回答 2

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; }
}
于 2012-08-23T14:29:54.137 回答
0

尝试.serialize()直接使用该方法。因此,如果我们假设您有一个包含以下内容的表单id="prm-form"

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

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

于 2012-08-23T14:44:28.093 回答