Creating a DropDownList

时光怂恿深爱的人放手 提交于 2019-12-24 03:24:43

问题


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.


回答1:


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" )


来源:https://stackoverflow.com/questions/23563138/creating-a-dropdownlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!