The standard DropDown helper doesn't support optgroup. However, next week I will release the new version of my Mvc Controls toolkit that contains a DropDown that supports optgroups and also individual styling of both options and optgroup, that is you can
give different colors or background colors to the various options and optgroups. See here:
http://mvccontrolstoolkit.codeplex.com/
Juest like Francesco said, The DropDownList cannot be used to generate the <select> tag with group, but we can create our own extension. The DropDownList accepts an IEnumerable<SelectListItem> , but we need to pass an IDictionary<string , IEnumerable<SelectListItem>>
to the dropdownlist where the key will be use for grouping the items in the DropDownList.
kobruleht
Member
589 Points
463 Posts
How to create dropdown menu list dynamically
Apr 15, 2011 08:44 PM|LINK
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.ViewModels.ShoppingCartViewModel>" %> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <div style="margin: 30px"> <% using (Html.BeginForm("AddressAndPayment", "Checkout")) { %> <table class="sum" style="border-collapse: collapse"> <colgroup> <col width="30%" /> <col /> </colgroup> <tbody> <tr> <td class="label"> <strong>Select delivery term</strong> </td> <td> <select name="delivery" id="delivery"> <option value="1" selected="selected">cargo</option> <option value="2">order</option> <option value="3">truck</option> <option value="4">in shop</option> </select> </td> </tr> <input class="button orange bigrounded" type="submit" style="color: white" id="cart-payment" value='go' /> <% } %> </div> </asp:Content>I added delivery term selection to MVC musicstore shopping cart into view using code above.For some products some delivery methods are not allowed.
How to change this code so that dropdown list in creatred in C# code and passed to view ?
How to get selected option if go button is pressed ?
Andrus.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: How to create dropdown menu list dynamically
Apr 15, 2011 09:17 PM|LINK
read this tutorial on dropdown: http://codeclimber.net.nz/archive/2009/08/10/how-to-create-a-dropdownlist-with-asp.net-mvc.aspx
Mvc Controls Toolkit | Data Moving Plug-in Videos
kobruleht
Member
589 Points
463 Posts
Re: How to create dropdown menu list dynamically
Apr 16, 2011 07:55 PM|LINK
Thank you. How to add option groups using the second method in article you suggested:
<select name="deliveryplace">
<option value="">-Select-</option>
<optgroup label="Region1">
<option value="133">City1</option>
<option value="136">City2</option>
</optgroup>
<optgroup label="Region2">
<option value="157">CityInRegion2</option>
....
Andrus.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: How to create dropdown menu list dynamically
Apr 17, 2011 12:04 AM|LINK
The standard DropDown helper doesn't support optgroup. However, next week I will release the new version of my Mvc Controls toolkit that contains a DropDown that supports optgroups and also individual styling of both options and optgroup, that is you can give different colors or background colors to the various options and optgroups. See here: http://mvccontrolstoolkit.codeplex.com/
Mvc Controls Toolkit | Data Moving Plug-in Videos
Forest Cheng...
Star
8370 Points
819 Posts
Re: How to create dropdown menu list dynamically
Apr 18, 2011 03:41 AM|LINK
Hi Kobruleht,
Juest like Francesco said, The DropDownList cannot be used to generate the <select> tag with group, but we can create our own extension. The DropDownList accepts an IEnumerable<SelectListItem> , but we need to pass an IDictionary<string , IEnumerable<SelectListItem>> to the dropdownlist where the key will be use for grouping the items in the DropDownList.
Look at this article:
http://weblogs.asp.net/raduenuca/archive/2011/02/26/asp-net-mvc-extending-the-dropdownlist-to-show-the-items-grouped-by-a-category.aspx
Hope this hlepful,
Forest Cheng
If you have any feedback about my replies,please contact msdnmg@microsoft.com.
Microsoft One Code Framework