我有一个Ajax调用,如下所示,从我的客户端js文件到MVC控制器方法
来自客户端js files ::
var ActivityLog = new Array();
var Test={}
Test['Customer[0].Id'] = "3060_3";
Test['Customer[0].Value'] = "0";
Test['CustId'] = "1111";
Test['crl'] = true;
Test['ActNumber'] = "2222";
for (var i = 0; i < 2; i++) {
Test['ActivityLog[' + i + ']'] ="1";
}
My Controller ActionMethod ::
public ActionResult SaveAccountCustomerServicePlans(string CustId, IDictionary<string, Customer> Customer, bool? crl, string ActNumber, string[] ActivityLog)
{
// Here all the parameter i am getting as null
}
我的实体类
public class Customer
{
[DataMember]
[Key]
public int Id{ get; set; }
[DataMember]
public string Value{ get; set; }
}
我的Ajax电话::
$.ajax({
type: 'POST',
async: true,
});
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader('__RequestVerificationToken', $(':input:hidden[name*="RequestVerificationToken"]').val());
},
type: "POST", url: "Customerss/Customermanagement/SaveAccountCustomerServicePlans", data: { Test: Test},
success: function (data) {
/////
}
});
在上面的控制器方法中,我得到的所有参数都为null。但是如果我从Test对象中删除ActivityLog,一切正常。任何人都可以帮我解决这个问题。
答案 0 :(得分:1)
试试这个,
更改您的控制器
public ActionResult SaveAccountCustomerServicePlans(Customer model, string CustId, bool? crl, string ActNumber, List<string> ActivityLog)
{
然后在javascript中
var ActivityLog = new Array();
var Test = {}
Test['Id'] = "30603";
Test['Value'] = "0";
Test['CustId'] = "1111";
Test['crl'] = true;
Test['ActNumber'] = "2222";
for (var i = 0; i < 2; i++) {
Test['ActivityLog[' + i + ']'] = "1";
}
最后在你的ajax中
type: "POST", url: "Customerss/Customermanagement/SaveAccountCustomerServicePlans", data: Test,
希望这能帮到你
注意:在你的Model类中,你有Id喜欢“int”,但在javascript中你发送的是一个字符串“30603_3”;
答案 1 :(得分:0)
在我喜欢下面之前删除加号,不知道为什么会有
Test['ActivityLog[' i + ']'] ="1";
如果您尝试将=“1”保存在下面,请单击或删除所有加号
Test['ActivityLog[' i ']'] ="1";