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.

有帮助吗?

解决方案

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
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top