I have a page with a table which list many results. I would like to introduce multiple dropdownlist to filter the results.
I would like to know if it could be better to create the List of SelectedItems from code behind and pass it through the viewModel or create the DropDownList directly on the Razor page?
The DropDownList will never change on this page.
Note that You can have many solutions but it is better for the separation of concern to code inside the controller. So you can put your list (for the dropdown) in a ViewBag
object and then in the View you just create a dropdown with this ViewBag
as SelectList. For more flexibility and respect of MVC pattern the view is just used to display data from the controller.
Here just an example:
In your Controller:
public ActionResult Index()
{
....
ViewBag.MyDropDownList1 = context.someTables.Select(t=>t);
}
And in your View:
@Html.DropDownListFor(m=>m.Somefields, ViewBag.MyDropDownList1 as SelectList,"Make the choice" )