0

This is my controller action:

public ActionResult BrowsePartial(IList<SearchParam> searchParams = null)
{
   //...
}

This is the object model:

public class SearchParam
{
    public string Order { get; set; }
    public string Type { get; set; }
    public string Value { get; set; }
}

And here is how i send data to controller:

$.ajax(
{
   type: "GET",
    url: url,
    data: { searchParams: [{ Order: "fghfdhgfdgfd", Type: "sasdsa", Value: "saddsadsads" }, { Order: "fghfdhgfdgfd", Type: "sasdsa", Value: "saddsadsads" }, { Order: "fghfdhgfdgfd", Type: "sasdsa", Value: "saddsadsads" }] },
    mode: "replace",
    cache: false,
 });

Now, when i debug the action, i have an IList<SearchParam> that is correctly initialized with 3 elements. However, fields of each SearchParam object (Order, Type and Value) are initialized to null. What could be the problem here?

7
  • your data should be like: data: { Order: "lala", Type: "lala2" } so drop the searchParams and the covering array [] Commented May 2, 2014 at 7:20
  • but it has to be an array of objects. not a single object! Commented May 2, 2014 at 7:21
  • is there any way to acomplish that? Commented May 2, 2014 at 7:22
  • it's an object literal => see my answer elsewhere Commented May 2, 2014 at 7:22
  • The way you wanna do it, is send 3 ajax requests at once. So you have to deal with your array first, then send it 3 times. Commented May 2, 2014 at 7:23

1 Answer 1

2

I think, the only way you can send your array parameter in a single request is to stringify it, and deserialize in your controller.

$.ajax(
{
   type: "GET",
    url: url,
    data: { searchParams: JSON.stringify([{ Order: "fghfdhgfdgfd", Type: "sasdsa", Value: "saddsadsads" }, { Order: "fghfdhgfdgfd", Type: "sasdsa", Value: "saddsadsads" }, { Order: "fghfdhgfdgfd", Type: "sasdsa", Value: "saddsadsads" }])},
    mode: "replace",
    cache: false,
 });


public ActionResult BrowsePartial(string searchParams = null)
{
    SearchParam params = JsonConvert.DeserializeObject<SearchParam>(searchParams);
}

But I maybe mistaken ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.