"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
==============================================
André Baltieri
São Paulo - Brazil
http://www.insidedotnet.com.br/
Twitter: @andrebaltieri
==============================================
As far as I know, You'll need to write your own extension method and extend SelectListItem to contain the grouping or generate the select manually in markup
Please double check my code.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
namespace System.Web.Mvc.Html
{
public static class GroupDropListExtensions
{
public static string GroupDropList(this HtmlHelper helper, string name, IEnumerable<GroupDropListItem> data, string SelectedValue, object htmlAttributes)
{
if (data == null && helper.ViewData != null)
data = helper.ViewData.Eval(name) as IEnumerable<GroupDropListItem>;
if (data == null) return string.Empty;
var select = new TagBuilder("select");
if (htmlAttributes != null)
select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
select.GenerateId(name);
var optgroupHtml = new StringBuilder();
var groups = data.ToList();
foreach (var group in data)
{
var groupTag = new TagBuilder("optgroup");
groupTag.Attributes.Add("label", helper.Encode( group.Name));
var optHtml = new StringBuilder();
foreach (var item in group.Items)
{
var option = new TagBuilder("option");
option.Attributes.Add("value", helper.Encode(item.Value));
if (SelectedValue != null && item.Value == SelectedValue)
option.Attributes.Add("selected", "selected");
option.InnerHtml = helper.Encode(item.Text);
optHtml.AppendLine(option.ToString(TagRenderMode.Normal));
}
groupTag.InnerHtml = optHtml.ToString();
optgroupHtml.AppendLine(groupTag.ToString(TagRenderMode.Normal));
}
select.InnerHtml = optgroupHtml.ToString();
return select.ToString(TagRenderMode.Normal);
}
}
public class GroupDropListItem
{
public string Name { get; set; }
public List<OptionItem> Items { get; set; }
}
public class OptionItem
{
public string Text { get; set; }
public string Value { get; set; }
}
}
Thanks for the snippit, Bober Song. I did find one little error. It appears that the TagBuilder class is already encoding it's attributes. Therefore, anyone using this code will want to remove the explicit encoding from the label attribute as follows:
longtqdayma
0 Points
11 Posts
Dropdownlist Optgroup with mvc
Sep 05, 2010 03:36 PM|LINK
"
Html.DropDownList() not support
Html.DropDownListFor() not support
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Dropdownlist Optgroup with mvc
Sep 05, 2010 05:32 PM|LINK
Search,
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
AndreBaltier...
Member
60 Points
15 Posts
Re: Dropdownlist Optgroup with mvc
Sep 06, 2010 01:55 AM|LINK
Also, you can create a custom helper for it...
http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs
Regards,
André Baltieri
São Paulo - Brazil
http://www.insidedotnet.com.br/
Twitter: @andrebaltieri
==============================================
Bober Song -...
All-Star
34686 Points
2167 Posts
Re: Dropdownlist Optgroup with mvc
Sep 09, 2010 05:54 AM|LINK
Hi longtqdayma ,
As far as I know, You'll need to write your own extension method and extend SelectListItem to contain the grouping or generate the select manually in markup
Please double check my code.
using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Routing; namespace System.Web.Mvc.Html { public static class GroupDropListExtensions { public static string GroupDropList(this HtmlHelper helper, string name, IEnumerable<GroupDropListItem> data, string SelectedValue, object htmlAttributes) { if (data == null && helper.ViewData != null) data = helper.ViewData.Eval(name) as IEnumerable<GroupDropListItem>; if (data == null) return string.Empty; var select = new TagBuilder("select"); if (htmlAttributes != null) select.MergeAttributes(new RouteValueDictionary(htmlAttributes)); select.GenerateId(name); var optgroupHtml = new StringBuilder(); var groups = data.ToList(); foreach (var group in data) { var groupTag = new TagBuilder("optgroup"); groupTag.Attributes.Add("label", helper.Encode( group.Name)); var optHtml = new StringBuilder(); foreach (var item in group.Items) { var option = new TagBuilder("option"); option.Attributes.Add("value", helper.Encode(item.Value)); if (SelectedValue != null && item.Value == SelectedValue) option.Attributes.Add("selected", "selected"); option.InnerHtml = helper.Encode(item.Text); optHtml.AppendLine(option.ToString(TagRenderMode.Normal)); } groupTag.InnerHtml = optHtml.ToString(); optgroupHtml.AppendLine(groupTag.ToString(TagRenderMode.Normal)); } select.InnerHtml = optgroupHtml.ToString(); return select.ToString(TagRenderMode.Normal); } } public class GroupDropListItem { public string Name { get; set; } public List<OptionItem> Items { get; set; } } public class OptionItem { public string Text { get; set; } public string Value { get; set; } } }http://kevindaly.spaces.live.com/blog/cns!EDA3C1275909727A!456.entry?wa=wsignin1.0&sa=709070164
I hope it is helpful to you.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
pankaj.ks
Member
298 Points
71 Posts
Re: Dropdownlist Optgroup with mvc
Sep 09, 2010 06:10 AM|LINK
Can you please explain your problem in more detail?
manjunatht
Member
84 Points
44 Posts
Re: Dropdownlist Optgroup with mvc
Jan 29, 2011 02:44 AM|LINK
Hi,
How do we call this in the View ?
vinneyk
Member
139 Points
92 Posts
Re: Dropdownlist Optgroup with mvc
Aug 04, 2011 04:31 AM|LINK
Thanks for the snippit, Bober Song. I did find one little error. It appears that the TagBuilder class is already encoding it's attributes. Therefore, anyone using this code will want to remove the explicit encoding from the label attribute as follows:
groupTag.Attributes.Add("label", group.Name)Thanks!
Vinney