I am new to MVC and I am trying to create my own extension method so that I can add onto the html helpers that are available in my razor views. Html.DropDownListFor()
lets you create a drop down list for any propery on your model. I would like to create a helper called Html.StateDropDownListFor()
that does the exact same thing, except loads the drop down with all 50 US states. This way I don't have to create a SelectList for every single state drop down that I create. What is the easiest way to do this? Right now I have this:
public static class ExtensionMethods
{
public static MvcHtmlString StateDropDownList(this HtmlHelper html)
{
// ???
}
}
Am I even close? I don't want to rebuild a whole text box helper, I just want to create a helper that utilizes the existing text box helper but does the SelectList for me. That way in my views I could just do Html.StateDropDownList(x => x.State)
Your answers are much appreciated.
Here's the answer!
You guys are a great help, thank you! Here is the completed extension method in case anyone else ever has a use for it.
public static MvcHtmlString StateDropDownListFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
Dictionary<string, string> stateList = new Dictionary<string, string>()
{
{"AL"," Alabama"},
{"AK"," Alaska"},
{"AZ"," Arizona"},
{"AR"," Arkansas"},
{"CA"," California"},
{"CO"," Colorado"},
{"CT"," Connecticut"},
{"DE"," Delaware"},
{"FL"," Florida"},
{"GA"," Georgia"},
{"HI"," Hawaii"},
{"ID"," Idaho"},
{"IL"," Illinois"},
{"IN"," Indiana"},
{"IA"," Iowa"},
{"KS"," Kansas"},
{"KY"," Kentucky"},
{"LA"," Louisiana"},
{"ME"," Maine"},
{"MD"," Maryland"},
{"MA"," Massachusetts"},
{"MI"," Michigan"},
{"MN"," Minnesota"},
{"MS"," Mississippi"},
{"MO"," Missouri"},
{"MT"," Montana"},
{"NE"," Nebraska"},
{"NV"," Nevada"},
{"NH"," New Hampshire"},
{"NJ"," New Jersey"},
{"NM"," New Mexico"},
{"NY"," New York"},
{"NC"," North Carolina"},
{"ND"," North Dakota"},
{"OH"," Ohio"},
{"OK"," Oklahoma"},
{"OR"," Oregon"},
{"PA"," Pennsylvania"},
{"RI"," Rhode Island"},
{"SC"," South Carolina"},
{"SD"," South Dakota"},
{"TN"," Tennessee"},
{"TX"," Texas"},
{"UT"," Utah"},
{"VT"," Vermont"},
{"VA"," Virginia"},
{"WA"," Washington"},
{"WV"," West Virginia"},
{"WI"," Wisconsin"},
{"WY"," Wyoming"},
{"AS"," American Samoa"},
{"DC"," District of Columbia"},
{"FM"," Federated States of Micronesia"},
{"MH"," Marshall Islands"},
{"MP"," Northern Mariana Islands"},
{"PW"," Palau"},
{"PR"," Puerto Rico"},
{"VI"," Virgin Islands"},
{"GU"," Guam"}
};
return html.DropDownListFor(expression, new SelectList(stateList, "key", "value"));
}
I modified the code above to use a dictionary for state abbreviations.
Just don't forget to reference System.Web.Mvc.Html
at the top of your extension method class like I forgot to, d'oh!
To use the custom helper method in your Razor views you will need to bring it into scope. There are two possible ways to do this:
@using SomeNamespace
in the top of your view with the namespace where the static class containing the helper is definedIn
~/Views/web.config
, add:Once the custom helper is brought into scope in the view, Intellisense should be able to pick it and you could use it:
Now you helper method needs to do something useful. You could either call existing helpers:
or return some custom data:
If you have a strongly typed view and you wanted to use an expression:
and then:
Would work.
_stateList
being anIEnumerable<SelectListItem>
.