0

I have a view, which consists of several textbox fields, used for searching/filtering and the Index method on the controller looks like this:

public ActionResult Index(string param1, string param2, string param3... string paramN)

This view receives a PagedList.IPagedList<FooViewModel>.

Based on filter/search using multiple fields, it recommends to use a ViewModel to search/filter. Which makes sense as it will make the code simple, and manageable.

Refactor result would look like this:

Index(FooSearchViewModel)

ViewModel:

class FooSearchViewModel{
public List<string> Filters {get;set;}
public string SearchValue {get;set;}
}

I want to remove all the search fields and replace with a DropDownListFor html helper, that has a list of filter options(param1,param2,param3...paramN)

And only one textbox field, in which a searchvalue will be input.

So the question is, how can I bind the FooSearchViewModel to the DropDownList helper in Razor instead of Paged.IPageList<FooViewModel>

Harry
  • 3,031
  • 7
  • 42
  • 67
  • A `DropDownListFor()` generates a ` –  Sep 08 '17 at 21:44
  • I want to pass back the ID of the selected option and value input into the single text field. – Harry Sep 08 '17 at 21:45
  • Then your view model needs 3 properties - `int SelectedFilter`, `IEnumerable FilterList` and `string SearchValue` (so that you can use `@Html.DropDownListFor(m => m.SelectedFilter, Model.FilterList)` in the view –  Sep 08 '17 at 21:48
  • is this going to be the searchviewmodel just to clarify? – Harry Sep 08 '17 at 21:49
  • Yes, and it would also include your `IPagedList` property for the paged list. Although its still not clear what your displaying in the dropdownlist - is that the list of column names? –  Sep 08 '17 at 21:51
  • Yes, it will be a list will contain column names in the dropdownlist. – Harry Sep 08 '17 at 21:53
  • Refer [this answer](https://stackoverflow.com/questions/42450257/how-to-i-apply-filter-while-paginating-in-asp-net-mvc-and-entity-framework/42450564#42450564) for an example - in that case its a dropdown to filter by a specific property value, but it would be a similar approach –  Sep 08 '17 at 21:54
  • thanks once again, it seems you always point me in the right directions on my recent questions :) – Harry Sep 08 '17 at 21:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153986/discussion-between-stephen-muecke-and-haris). –  Sep 08 '17 at 21:56
  • Just to clarify as well, I do not need to apply pagination while filtering – Harry Sep 08 '17 at 21:57

0 Answers0