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

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

问题描述

我是 MVC 的新手,我正在尝试创建我自己的扩展方法,以便我可以添加到我的 razor 视图中可用的 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)
    {
        // ???
    }
}

我真的很亲近吗?我不想重建整个文本框助手,我只想创建一个使用现有文本框助手但为我执行 SelectList 的助手.这样在我看来我可以只做 Html.StateDropDownList(x => x.State)

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)

推荐答案

要在 Razor 视图中使用自定义帮助器方法,您需要将其引入作用域.有两种可能的方法来做到这一点:

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. ~/Views/web.config中,添加:

  1. Add a @using SomeNamespace in the top of your view with the namespace where the static class containing the helper is defined
  2. In ~/Views/web.config, add:

<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 应该能够选择它并且您可以使用它:

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()

现在你的辅助方法需要做一些有用的事情.您可以调用现有的助手:

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

如果你有一个强类型视图并且你想使用一个表达式:

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()

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

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