4

In my controller I have:

[AcceptVerbs("GET", "POST")]
    public List<BuzzMonitor.Web.Message> Search(string text, DateTime dateFrom, DateTime dateTo, List<int> themeIds, List<int> sourceIds) 
    {...}

and I want to do model binding. It's easy for primitive types, but what to do when I have a list of primitive types?

I did it like this in Global.asax:

 GlobalConfiguration.Configuration.Routes.MapHttpRoute("SearchWithParameters", "api/{controller}/{action}/{text}/{dateFrom}/{dateTo}/?/?"

But I dont know what to set for lists...

I found on some site that I can add [ModelBinder] before list, but when I do that I just get red underline on that word.

Does anyone have an idea how to do it?

1
  • Ohh, I was in hurry! Thank you for edit! @SpaceBison But does anyone have idea how to solve it? Commented May 29, 2014 at 12:51

1 Answer 1

5

From your description it looks like you have found this article or one like it

http://lostechies.com/keithdahlby/2012/10/04/asp-net-web-api-list-parameter-binding/

recommending you use the ModelBinder attribute. I still recommend this way if you can get it to work. The red underline you describe sounds like you may not have the appropriate references. Please make sure you have the appropriate references in your class to access that attribute, in this case it looks like System.Web.Http.ModelBinding

http://msdn.microsoft.com/en-us/library/system.web.http.modelbinding.modelbinderattribute(v=vs.118).aspx

If that fails you will likely not be able to use Model Binding. From the first article

Web API only uses model binding for “simple types”

you can also look at using a JSON Formatter or similar, this is not difficult and would easily support List structures, with well formatted JSON.

here is a great introductory article to using this

http://www.hanselman.com/blog/OneASPNETMakingJSONWebAPIsWithASPNETMVC4BetaAndASPNETWebAPI.aspx

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

2 Comments

I try it now, and I dont have underlining anymore :D still I have a problem in GLobal.asax because I am not sure how to write list parameter. If I do it like this: GlobalConfiguration.Configuration.Routes.MapHttpRoute("SearchWithParameters", "api/{controller}/{action}/{text}/{dateFrom}/{dateTo}/[themeIds]/[sourceIds]" , I am still not sure how to test it in browser. I tried this format http://localhost:51041/api/Values/Search/buzz/2014-05-29/2014-05-30/[1,2]/[2,3] but I got 404 errror. Now problem can be in Global.asax or in browser query or both. Do you know something about this?
Ohh, my mistake! I solve it in the easiest way! I put: GlobalConfiguration.Configuration.Routes.MapHttpRoute("SearchWithParameters", "api/{controller}/{action}/{text}/{dateFrom}/{dateTo}/{themeIds}/{sourceIds}" And simple query: http://localhost:51041/api/Values/Search/buzz/2014-05-29/2014-05-30/1/2 and it passes! Thank you for help!

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.