views:

149

answers:

4

Hi

I am using asp.net mvc and I am trying to send 2 sets of database from jquery to my Mvc view.

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.

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.

$.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)

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.

Thanks

A: 

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?

Ropstah
I am not sure what you mean "you know what's going on right?"
chobo2
You 'know' which array is which in the returned json. Imagine receiving the following object: array('message' => 'success', 'data' => array('dataset1' => array(data), 'dataset2' => array(data2))
Ropstah
A: 

Since serializeArray will return you an array of name/value pair objects, you could simply concatenate the two arrays with Array.concat:

var send = array1.concat(array2);
CMS
hmm that might works but how can I serializeArray fields that are not in a form. How would I make this array out of these fields?
chobo2
I have 3 values that I grabed from a textboxes sitting in memory. How owuldI make them into a serializedArray?
chobo2
A: 

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. You can instantiate the two model types and use the TryUpdateModel() method with each model individually. I have used this and it works as all fields from both models are retained in the form post.

CmdrTallen
A: 

Does no one else wonder why the code has a post to an ActionResult View instead of someother like JSon etc?

Hurricanepkt