Chaddeus
Chaddeus

Reputation: 13356

Best way to add "filter" options to MVC action, for new Web API feature of ASP.NET MVC 4

The new Web API "stuff" looks great. I will be re-writing the foundation of my site into an API, but have a couple scenarios that I've got concern with.

Is it a bad idea to have an action that looks something like:

GetProducts(long memberId, string categories, int minPrice, int maxPrice, ...)

Where each variable is something the product can be filtered by. If the variable is null/empty it wouldn't use them to build the query.

Or is there another technique for reaching the same goal?

Upvotes: 0

Views: 366

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could use a view model:

public class FilterViewModel
{
    public long MemberId { get; set; }
    public string Categories { get; set; }
    public int MinPrice { get; set; }
    public int MaxPrice { get; set; }
    ...
}

and then:

public IEnumerable<ProductViewModel> GetProducts(FilterViewModel filter)
{
    ...    
}

Upvotes: 1

Related Questions