0

I'm fairly new to ASP.NET MVC4 and I have a search/filter form where you can filter on multiple parameters

So this is my controller

public ActionResult Index(string page, int? neighborhoodID, int? accommodationType) {
...
}

I was thinking. I'm using data annotations and validation for my login/registering by using the Model class.

Is there a way I could filter values using the Model class?

Now I just look at the requested parameters and use them in my linq query to get the filtered records.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
t0tec
  • 330
  • 4
  • 16
  • Your question is a little vague. What's wrong with the way you are doing it now? – Robert Harvey Jan 02 '13 at 20:32
  • Well I have a function: public static List GetAccommodationsByNeighborhoodIdAndAccType(int nId, int accType) that queries the database (with the filtered values) and if I would add more values this could get complicated. And I don't know if this is the way to do it. – t0tec Jan 02 '13 at 20:43
  • why don't you create a `InputModel` and use it as a single parameter? I mean, a class with theses properties and if you need other properties, you can just add new properties and it will work fine. – Felipe Oriani Jan 02 '13 at 21:03
  • I don't understand so I would have this: public class InputModel { public int NeighborhoodId { get; set; } public int AccommodationType { get; set; } } and how would I filter now? – t0tec Jan 02 '13 at 21:24

1 Answers1

1

I think you should create an IndexViewModel class

public class IndexViewModel
{
    public int? NeighbourhoodId { get; set; }
    public int? AccomodationType { get; set; }  
}

Then add @model IndexViewModel to the top of your view

It seems that neigbourhoodId and accomodationType come from dropdowns, so map viewModel properties to those dropdowns

and then the controller method will be somehting like this:

public ActionResult Index(string page, IndexViewModel model) 
{
    // You can use model.NeighbourhoodId and model.AccomodationType the same way you did with parameters
}
Esteban Elverdin
  • 3,552
  • 1
  • 17
  • 21