在ASP.NET MVC 3中添加您自己的HtmlHelper
我是新来的MVC,我想创build我自己的扩展方法,以便我可以添加到我的剃刀视图可用的HTML帮助器。 Html.DropDownListFor()
让你创build一个下拉列表模型上的任何propery。 我想创build一个名为Html.StateDropDownListFor()
的帮助器,它完成同样的事情,除了加载与美国所有50个州的下拉菜单。 这样我就不必为每个创build的状态下拉菜单创build一个SelectList。 什么是最简单的方法来做到这一点? 现在我有这个:
public static class ExtensionMethods { public static MvcHtmlString StateDropDownList(this HtmlHelper html) { // ??? } }
我甚至closures? 我不想重build一个完整的文本框帮助器,我只是想创build一个帮助器,利用现有的文本框助手,但为我做SelectList。 这样在我的意见,我可以做Html.StateDropDownList(x => x.State)
你的回答非常感谢。
这是答案!
你们是一个很好的帮助,谢谢! 这是完成的扩展方法,以防其他人使用它。
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")); }
我修改了上面的代码来使用状态缩写字典。
只要不要忘记在你的扩展方法类的顶部引用System.Web.Mvc.Html
,就像我忘了,哦!
要在Razor视图中使用自定义帮助程序方法,您需要将其纳入范围。 有两种可能的方法来做到这一点:
- 在视图的顶部添加一个
@using SomeNamespace
,并在其中定义包含帮助器的静态类的名称空间 -
在
~/Views/web.config
,添加:<system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="SomeNamspace" /> </namespaces> </pages> </system.web.webPages.razor>
一旦自定义助手被引入到视图的范围内,Intellisense应该能够select它,并且可以使用它:
@Html.StateDropDownList()
现在你的帮手方法需要做一些有用的事情。 你可以打电话给现有的帮手:
public static class ExtensionMethods { public static MvcHtmlString StateDropDownList(this HtmlHelper html) { return html.TextBox("foo") } }
或返回一些自定义数据:
public static class ExtensionMethods { public static MvcHtmlString StateDropDownList(this HtmlHelper html) { return MvcHtmlString.Create("Hello world"); } }
如果你有一个强types的视图,你想使用一个expression式:
using System.Web.Mvc; using System.Web.Mvc.Html; public static class ExtensionMethods { public static MvcHtmlString StateDropDownList( this HtmlHelper<MyViewModel> html ) { var stateList = new SelectList(new[] { new { Key = "Alabama", Value = "Alabama" }, new { Key = "Idaho", Value = "Idaho" }, new { Key = "California", Value = "California" } }, "Key", "Value"); return Html.DropDownListFor( x => x.State, stateList, "-- Select a state --" ); } }
接着:
@Html.StateDropDownList()
using System.Web.Mvc.Html; public static MvcHtmlString StateDropDownList<TModel, TValue>( this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression ) { return html.DropDownListFor( expression, _stateList ); }
会工作。 _stateList
是一个IEnumerable<SelectListItem>
。