Data Bound DropDownList (HTML Helper) for ASP.NET MVC

In this Post I will try to create a HTML Helper method to create a Data-bound Dropdownlist for ASP.NET MVC framework. When iterating through the DataSource in the ViewPage, to create a Dropdownlist is easy, but at the sametime this is one feature that we will come across multiple time during any project execution. So it make more sense to me, if we create a HTML Helper, which will generate the  Dropdownlist from a DataSource.

Our HTML Helper will accept the following parameters.

  1. A DataSource
  2. DataTextField
  3. DataValueField
  4. Selectedvalue
  5. HTMLAttributes, this will be optional.
   1: public static string DataBoundDropDownList(this HtmlHelper helper, object DataSource,string DataValueProperty,string DataTextProperty,string selectedValue, object HtmlAttributes)
   2: public static string DataBoundDropDownList(this HtmlHelper helper, object DataSource, string DataValueProperty, string DataTextProperty, string selectedValue)

So there will be two methods, one with the HTMLAttribute parameter another without it. In this walkthrough we will use TagBuilder to generate the HTML, rather than using StringBuilder. Since in both of the methods the <option> tag will be same, so we will be using a private method to generate it.

   1: private static TagBuilder CreateDropDownlist(object DataSource, string DataValueProperty, string DataTextProperty, string selectedValue)

This method will return me a TagBuilder of type “select”, which will contain all the “<option>” tag for the select tag.

At first in CreateDropDownlist method we will create the “select” tag.

   1: TagBuilder selectBuilder = new TagBuilder("select");

Then we will be iteration through the DataSource, to create the options fro the dropdownlist. So the code for the CreateDropDownlist will look

   1: private static TagBuilder CreateDropDownlist(object DataSource, string DataValueProperty, string DataTextProperty, string selectedValue)
   2:         {
   3:             #region "Create the DropdownList"
   4:             TagBuilder selectBuilder = new TagBuilder("select");
   5:             StringBuilder optionStringBuilder = new StringBuilder();
   6:             #endregion
   7:  
   8:             #region "Create OptionList"
   9:             IEnumerable ddlSource = DataSource as IEnumerable;
  10:             if (ddlSource == null)
  11:             {
  12:                 throw new Exception("DataSource must implement IEnumerable Interface.");
  13:             }
  14:             IEnumerator item = ddlSource.GetEnumerator();
  15:             while (item.MoveNext())
  16:             {
  17:                 string strText = DataBinder.GetPropertyValue(item.Current, DataTextProperty).ToString();
  18:                 string strVal = DataBinder.GetPropertyValue(item.Current, DataValueProperty).ToString();
  19:                 TagBuilder optionBuilder = new TagBuilder("option");
  20:                 optionBuilder.MergeAttribute("value", strVal);
  21:                 optionBuilder.InnerHtml = strText;
  22:                 if (strVal == selectedValue)
  23:                 {
  24:                     optionBuilder.MergeAttribute("selected", "selected");
  25:                 }
  26:                 optionStringBuilder.Append(optionBuilder.ToString());
  27:             }
  28:             selectBuilder.InnerHtml = optionStringBuilder.ToString();
  29:             #endregion
  30:  
  31:             return (selectBuilder);
  32:         }

 

Now once we get back the TagBuilder from the CreateDropDownlist method, in the DataBoundDropDownList method we will do the following, depending on the no of parameters.

   1: public static string DataBoundDropDownList(this HtmlHelper helper, object DataSource, string DataValueProperty, string DataTextProperty, string selectedValue)
   2:         {
   3:             TagBuilder selectTag = CreateDropDownlist(DataSource, DataValueProperty, DataTextProperty, selectedValue);
   4:             return (selectTag.ToString());
   5:         }

 

Or

   1: public static string DataBoundDropDownList(this HtmlHelper helper, object DataSource,string DataValueProperty,string DataTextProperty,string selectedValue, object HtmlAttributes)
   2:         {
   3:             TagBuilder selectTag = CreateDropDownlist(DataSource, DataValueProperty, DataTextProperty, selectedValue);
   4:             selectTag.MergeAttributes(new RouteValueDictionary(HtmlAttributes));
   5:             return (selectTag.ToString());
   6:         }

That’s all !!!. Have fun.

Source

6 Comments

Comments have been disabled for this content.