在 ASP.NET MVC 3 中添加您自己的 HtmlHelper
Posted
技术标签:
【中文标题】在 ASP.NET MVC 3 中添加您自己的 HtmlHelper【英文标题】:Adding your own HtmlHelper in ASP.NET MVC 3 【发布时间】:2011-06-30 11:30:43 【问题描述】:我是 MVC 的新手,我正在尝试创建自己的扩展方法,以便可以添加到我的 razor 视图中可用的 html 帮助程序中。 Html.DropDownListFor()
允许您为模型上的任何属性创建一个下拉列表。我想创建一个名为Html.StateDropDownListFor()
的助手,它做同样的事情,除了加载美国所有 50 个州的下拉菜单。这样我就不必为我创建的每个状态下拉列表创建一个 SelectList。最简单的方法是什么?现在我有这个:
public static class ExtensionMethods
public static MvcHtmlString StateDropDownList(this HtmlHelper html)
// ???
我什至接近吗?我不想重建整个文本框助手,我只想创建一个利用现有文本框助手但为我执行 SelectList 的助手。在我看来,这样我就可以做到Html.StateDropDownList(x => x.State)
【问题讨论】:
【参考方案1】:(代表问题作者发布解决方案,将其移至答案部分)。
这就是答案!
这是完整的扩展方法,以防其他人使用它。
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
,就像我忘记的那样。
【讨论】:
【参考方案2】:要在 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 应该能够选择它并且您可以使用它:
@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");
如果你有一个强类型视图并且你想使用表达式:
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()
【讨论】:
我想你误会了。在我看来,我知道如何使用扩展方法。我只是不知道如何在扩展方法中利用现有的TextBoxFor
助手来创建我自己的助手,建立在现有助手的基础上。
宾果游戏,我错过了using System.Web.Mvc.Html
,无论出于何种原因,它都不会自动解决。谢谢!
我在 HTML 页面中得到一个运行时错误,因为它是在没有参数的情况下调用的,但定义包括 1,它是 this HtmlHelper helper。你知道为什么吗?
除了项目中的自定义 Html 助手之外,您在哪里?如问here?
@Khnle-Kevin - 您需要包含它适用的模型属性:@Html.StateDropDownListFor(m => m.State)
【参考方案3】:
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>
。
【讨论】:
这更接近,但DropDownListFor
不是html
在扩展方法上下文中的属性:/
添加using System.Web.Mvc.Html;
以使扩展方法可用。以上是关于在 ASP.NET MVC 3 中添加您自己的 HtmlHelper的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ASP.NET MVC 中将条带支付与自定义表单集成? [关闭]
如何在 ASP.NET MVC 5.2.3 和 Katana 中正确设置双重身份验证?
如何将 Web API 添加到现有的 ASP.NET MVC (5) Web 应用程序项目中?
007.Adding a view to an ASP.NET Core MVC app -- 在asp.net core mvc中添加视图