1

I am allowing JsonValueProviderFactory to populate the parameters of my controller action. One of the parameters is an array of Cat. This works, but sometimes when there is only a single Cat it doesn't get passed in as an array, the argument is just Cat. I am using ExtJs, which is handling everything clientside.

Is there a way to handle this server-side? Or will I need to hack the request from ExtJs to force it to always send an array?

    [HttpPost]
    public ActionResult Edit(int id, IEnumerable<Cat> Data){...}

2 Answers 2

1

A proper JSON request should look like this if you have a single cat:

{ 'id': '123', 'Data': [ { 'Name': 'Felix', 'Age': 6 } ] }

and for multiple cats:

{ 'id': '123', 'Data': [ { 'Name': 'Felix', 'Age': 6 }, { 'Name': 'Morgan', 'Age': 2 } ] }
Sign up to request clarification or add additional context in comments.

Comments

0

MVC is attempting to bind the single instance of Cat to just that, an instance of Cat and not IEnumerable. Pass the array.

If you want to handle it server side, you could always:

List<Cat> cats = new List<Cat>;
cats.Add(argCat);
...

Since your controller argument is IEnumerable, it would seem you're expecting a set of data anyway. Maybe consider a separate controller action for the single instance if it makes business sense.

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.