简体   繁体   English

如何使用Jquery Ajax.Post发送2组数据

[英]How to send 2 sets of data back with Jquery Ajax.Post

I am using asp.net mvc and I am trying to send 2 sets of database from jquery to my Mvc view. 我正在使用asp.net mvc,并且尝试从jquery发送2套数据库到我的Mvc视图。

Now my view will look like this 现在我的观点看起来像这样

public ActionResult MyView(Product one, Product two)
{
}

now how do I send the stuff back so everything will be binded correctly? 现在我该如何将东西寄回,以便一切都可以正确绑定? I know how to do it with one set of data but not with 2. 我知道如何用一组数据,但不与2去做。

Like if I only need to past one set of data to one Product prameter all I would do this 就像我只需要将一组数据粘贴到一个产品参数上一样,我就会这样做

var send = form.serializeArray();

and in my ajax post. 在我的ajax帖子中

$.post("MyView",  send, function(result) {});

It would be sent to MyView and it wold be bound to Product1(note in this case MyView would only have one parameter) 它将被发送到MyView,并将其绑定到Product1(请注意,在这种情况下,MyView仅具有一个参数)

But I don't know how to do thsi with 2 serializedArrays since it seems I would have to json this stuff up and then the binder can't figure out what is going on. 但我不知道该怎么办thsi 2个serializedArrays因为它似乎我不得不起来JSON这个东西,然后粘结剂也弄不清是怎么回事。

Thanks 谢谢

You can create an object with two inner arrays and serialize that? 您可以创建具有两个内部数组的对象并将其序列化吗? In the end you are using the returned json and you know what's going on right? 最后,您使用的是返回的json,您知道发生了什么吗?

由于serializeArray将返回一个名称/值对对象数组,因此您只需将两个数组与Array.concat连接即可:

var send = array1.concat(array2);

Not sure I understand, but I will give it a shot. 不确定我是否理解,但是我会试一试。 If you have two models you are dealing with in your view and you want to update both models in your Controller. 如果您有两个模型在视图中处理,则想在Controller中更新两个模型。 You can instantiate the two model types and use the TryUpdateModel() method with each model individually. 您可以实例化这两种模型类型,并分别对每个模型使用TryUpdateModel()方法。 I have used this and it works as all fields from both models are retained in the form post. 我已经使用了它,并且由于两个模型的所有字段都保留在表单发布中,因此可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM