在 Ajax 调用中将对象内的数组传递给 MVC 控制器
Pass an array within an object in Ajax call to MVC controller
我有一个 Ajax 从我的客户端 js 文件到 MVC acontroller 方法的调用
来自客户端js文件::
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
}
我的实体class
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 。但是如果我从测试对象中删除 ActivityLog 一切正常。谁能帮我解决这个问题。
在我喜欢下面之前删除加号不知道为什么它在那里
Test['ActivityLog[' i + ']'] ="1";
或者如果您像下面那样尝试在此处保存 ="1",请将加号一起删除
Test['ActivityLog[' i ']'] ="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,
希望对您有所帮助
注意:在您的模型 class 中您有 Id like "int" 但在 javascript 中您发送的是字符串“30603_3”;
我有一个 Ajax 从我的客户端 js 文件到 MVC acontroller 方法的调用
来自客户端js文件::
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
}
我的实体class
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 。但是如果我从测试对象中删除 ActivityLog 一切正常。谁能帮我解决这个问题。
在我喜欢下面之前删除加号不知道为什么它在那里
Test['ActivityLog[' i + ']'] ="1";
或者如果您像下面那样尝试在此处保存 ="1",请将加号一起删除
Test['ActivityLog[' i ']'] ="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,
希望对您有所帮助
注意:在您的模型 class 中您有 Id like "int" 但在 javascript 中您发送的是字符串“30603_3”;