添加自己的HtmlHelper在ASP.NET MVC 3 [英] Adding your own HtmlHelper in ASP.NET MVC 3

查看:105
本文介绍了添加自己的HtmlHelper在ASP.NET MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC和我试图创建自己的扩展方法,这样我可以添加到HTML助手是我的剃须刀视图中使用。 Html.DropDownListFor()允许您创建一个下拉列表供您模型中的任何属性格式。我想创建一个名为帮手 Html.StateDropDownListFor(),做同样的事情,除了负载下拉与美国50个州。这样,我没有为每一个状态下拉,我创建创建的SelectList。什么是做到这一点的最简单的方法?现在,我有这样的:

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)
    {
        // ???
    }
}

我是差得远了?我不想重建整个文本框的帮手,我只是想创建一个利用现有的文本框中帮手但不会选择列表的我的一个帮手。在我的意见这种方式,我可以只是做 Html.StateDropDownList(X => x.State)

您的答案是多少AP preciated。

Your answers are much appreciated.

这里的答案!

你们是一个很大的帮助,谢谢!这里是万一别人有史以来完成扩展方法已经为它用。

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"));
    }

我修改了上面的code使用字典状态的缩写。

只是不要忘了参考 System.Web.Mvc.Html 在您的分机方法类的顶部像我忘了,德哦!

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:


  1. 在与命名空间的视图,是指含有辅助静态类的顶部添加 @using SomeNamespace

  2. 〜/查看/ 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>


在自定义的助手被带入范围在视图中,智能感知应能选择它,你可以使用它:

Once the custom helper is brought into scope in the view, Intellisense should be able to pick it and you could use it:

@Html.StateDropDownList()

现在你的helper方法需要做一些有用的东西。你既可以调用现有的助手:

Now you helper method needs to do something useful. You could either call existing helpers:

public static class ExtensionMethods
{
    public static MvcHtmlString StateDropDownList(this HtmlHelper html)
    {
        return html.TextBox("foo")
    }
}

或返回一些自定义的数据:

or return some custom data:

public static class ExtensionMethods
{
    public static MvcHtmlString StateDropDownList(this HtmlHelper html)
    {
        return MvcHtmlString.Create("Hello world");
    }
}

如果您有一个强类型的视图,你想使用一个前pression:

If you have a strongly typed view and you wanted to use an 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()

这篇关于添加自己的HtmlHelper在ASP.NET MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆