[转]ASP.NET MVC Select List Example
2013-07-05 18:45
197 查看
本文转自:http://www.aspnetmvcninja.com/views/asp-net-mvc-select-list-example
Select lists are a great way to allow users to select multiple options from a long list of possible values. But how do you implement a select list in ASP.NET MVC? Luckily ASP.NET MVC does most of the heavy lifting for you. For this example I’m going to use a product that has multiple categories.
I’ll start by defining two model classes. The first class is called Category and I’ll use a list of them to store the possible values that will be displayed in the select list. For this example I’ll manually populate these in the controller but you could just as easily load them from a database. The other class is Product which has many category ID’s stored in the CategoryID property (a collection of int’s).
Next we need a controller.
I’ve created a private method called GetOptions() to populate a list with the categories. In real world applications you’ll have a service (business logic) class that provides these.
GET requests are handled by the first Index action. It passes the select list options to the view using the ViewBag. You could use ViewData just as easily. It also creates a new Product which becomes the model for the view. If you want to pre-select values then simply add them as follows:
The second Index action handles POST requests. The values for product are populated by the default binder that comes with ASP.NET MVC. This will add all of the values the user selected on the form so you don’t need to do anything.
Finally I have a view that displays the selected values as an unordered list with a select list below it allowing you to change the selected options.
In the view I’m using the Html.ListBoxFor() helper to render the select list. The second value passed to this is a list of options which I’m building using the MultiSelectList class. The constructor I’m using accepts 4 parameters:
A list of options. In our example it’s List<Category>.
The name of the property that contains the value used in the select list. I’m using “ID” here because I want to use the ID property of Category.
The name of the property the contains the text to use for each option. I’m using “Name” here because I want to use the Name property of Category.
A list of currently select values.
You may be wondering why I pass a list of Category objects to the view instead of creating the MultiSelectList in the controller and then passing that to the view. Personally I find creating the MultiSelectList in the controller both makes the controller more complicated and mixes view logic with controller logic. I also find this approach works better in real world applications where you are probably getting the list of options from a service class. By doing it this way it’s possible to mock the service and test that the value returned by the service class is passed to the view correctly when writing unit tests.
Here’s what it looks like when you first display the select list…
… and after you’ve selected some values
- See more at: http://www.aspnetmvcninja.com/views/asp-net-mvc-select-list-example#sthash.bzAba6tW.dpuf
Select lists are a great way to allow users to select multiple options from a long list of possible values. But how do you implement a select list in ASP.NET MVC? Luckily ASP.NET MVC does most of the heavy lifting for you. For this example I’m going to use a product that has multiple categories.
I’ll start by defining two model classes. The first class is called Category and I’ll use a list of them to store the possible values that will be displayed in the select list. For this example I’ll manually populate these in the controller but you could just as easily load them from a database. The other class is Product which has many category ID’s stored in the CategoryID property (a collection of int’s).
GET requests are handled by the first Index action. It passes the select list options to the view using the ViewBag. You could use ViewData just as easily. It also creates a new Product which becomes the model for the view. If you want to pre-select values then simply add them as follows:
Finally I have a view that displays the selected values as an unordered list with a select list below it allowing you to change the selected options.
A list of options. In our example it’s List<Category>.
The name of the property that contains the value used in the select list. I’m using “ID” here because I want to use the ID property of Category.
The name of the property the contains the text to use for each option. I’m using “Name” here because I want to use the Name property of Category.
A list of currently select values.
You may be wondering why I pass a list of Category objects to the view instead of creating the MultiSelectList in the controller and then passing that to the view. Personally I find creating the MultiSelectList in the controller both makes the controller more complicated and mixes view logic with controller logic. I also find this approach works better in real world applications where you are probably getting the list of options from a service class. By doing it this way it’s possible to mock the service and test that the value returned by the service class is passed to the view correctly when writing unit tests.
Here’s what it looks like when you first display the select list…
… and after you’ve selected some values
- See more at: http://www.aspnetmvcninja.com/views/asp-net-mvc-select-list-example#sthash.bzAba6tW.dpuf
相关文章推荐
- 一段用于生成 ASP.NET MVC 中 DropDownListFor 的 SelectListItem 可枚举的集合
- asp.net mvc SelectList 的selected 失效及解决方案
- ASP.NET.MVC--注解和反射将Enum转换List<SelectListItem>为@Html.DropDownListFor()下拉列表的通用方法
- 【翻译】ASP.NET MVC:转化业务对象到Select List Items上
- asp.net mvc SelectList 的selected 失效及解决方案
- asp.net mvc SelectList使用
- 一段用于生成 ASP.NET MVC 中 DropDownListFor 的 SelectListItem 可枚举的集合
- asp.net mvc SelectList 的selected 失效及解决方案
- asp.net mvc SelectList 的selected 失效及解决方案
- asp.net mvc SelectList 的selected 失效及解决方法
- 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary
- asp.net MVC dropList 绑定
- ASP.NET MVC 3 CheckBoxList 的使用
- session + List<T> 实现购物车(asp.net mvc)
- ASP.NET MVC利用PagedList分页(二)PagedList+Ajax+JsRender
- 在ASP.NET MVC中使用PagedList之2--将查询关键字传入后端
- 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary
- ASP.NET MVC 3 CheckBoxList 的使用
- asp.net mvc中构建htmlHelper的checkboxlist
- Enhanced PagedList for ASP.NET MVC