Domanda

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.

È stato utile?

Soluzione

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
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top