简体   繁体   English

如何通过Ajax POST将数据绑定到Action方法的两个复杂类型参数

[英]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.

相关问题 我怎样才能将这种复杂类型的AJAX POST到我的MVC动作中? - How can I AJAX POST this complex type to my MVC action? 在动作方法中,如何将发布数据绑定到动态对象? - In an action method, how can I bind post data to a dynamic object? 如何将具有 ajax 的复杂数据类型发送到 ASP.NET MVC 中 controller 中的操作 - How send complex data type with ajax to the action in controller in ASP.NET MVC 我如何在数组中发布乘法对象并在action方法中获取数据 - How can i post multiply objects in array and get the data in the action method 如何在 MVC4 中绑定包含复杂类型的列表模型? - How can I bind a list model which contains a complex type in MVC4? 如何在提交表单发布方法之前将复杂数据类型附加到视图模型 - How to append Complex Data Type to View Model before submit of form post method 为什么不能将表单数据直接绑定到操作方法参数? - Why can't I bind form data directly to action method parameter? 如何发布一个复杂的模型来行动? - How to post a complex model to action? 如何修改此扩展方法接受两个字符串参数? - How can I modify this extension method accept two string parameters? 如何在控制器的操作方法中重定向Ajax发布? - How do I do a redirect an Ajax Post in a Controller's Action Method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM