我是MVC的新手,我正在尝试创建自己的扩展方法,以便我可以添加到我的剃刀视图中可用的html帮助器。 Html.DropDownListFor()
可让您为模型上的任何属性创建下拉列表。我想创建一个名为Html.StateDropDownListFor()
的帮助程序,完成相同的操作,除了加载美国所有50个州的下拉列表。这样我就不必为我创建的每个状态下拉列表创建一个SelectList。最简单的方法是什么?现在我有这个:
public static class ExtensionMethods
{
public static MvcHtmlString StateDropDownList(this HtmlHelper html)
{
// ???
}
}
我甚至关闭了吗?我不想重建整个文本框助手,我只是想创建一个利用现有文本框助手的助手,但是为我做了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
,就像我忘了一样,噢!
答案 0 :(得分:70)
要在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()
答案 1 :(得分:8)
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>
。
答案 2 :(得分:0)
(代表问题作者发布了一个解决方案,将其移至答案部分)。
这是答案!
这是完整的扩展方法,以防其他人使用。
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
。