[英]How can I bind data to two complex type parameters of Action method by Ajax POST
So far I have never had the need to send data from ajax to two or more complex type parameters in the server method. 到目前为止,我从未需要在服务器方法中将数据从ajax发送到两个或更多个复杂的类型参数。
What I am trying to achieve is if I had this action on my controller: 我要实现的是,如果我对控制器执行了以下操作:
[HttpPost]
[Authorize]
public virtual ActionResult SubmitData(Person myPerson, Chair myChair)
{
//..
}
where Person and Chair are complex types, I want to be able to send data from ajax that is going to bind properly to myPerson and myChair. 在Person和Chair是复杂类型的地方,我希望能够从ajax发送数据,该数据将正确绑定到myPerson和myChair。 Something like this:
像这样:
var personData = { 'Name': Steve, 'Age': 35};
var chairData = {'NumberOfLegs' : 3, 'Color' : red};
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: myUrl,
data: {'myPerson':personData, 'myChair':chairData}
});
I tried so, but it just wont work. 我试过了,但是它行不通。 Can you help me with that?
你能帮我吗? The call goes to the action but with null values for both arguments.
调用转到操作,但是两个参数的值均为空。
I have modified the ajax call to use JSON.stringify the parameters works well now. 我已经修改了使用JSON.stringify的ajax调用,该参数现在运行良好。
var personData = { 'Name': Steve, 'Age': 35};
var chairData = {'NumberOfLegs' : 3, 'Color' : red};
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: myUrl,
data: JSON.stringify({'myPerson':personData, 'myChair':chairData})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.