创建视图中可用的自定义帮手 [英] Creating custom helper available in views

查看:165
本文介绍了创建视图中可用的自定义帮手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有太多的文字工具方法,如 MakeShortText(字符串文字,诠释长度) RemoveTags(字符串文本) TIMEAGO(DateTime的日期)等。
我想从不同的助手来访问他们喜欢在明年例如:

I have too much text utility methods, like MakeShortText(string text, int length), RemoveTags(string text), TimeAgo(DateTime date) and other. I want to access them from separate helper like in next example:

@Text().MakeShortText(Model.Text, 10)

是否有可能创造这样的扩展?或者,我必须做出扩展的HtmlHelper是这样的:

Is it possible to create such extension? Or I have to make extension for HtmlHelper like this:

@Html.Text().MaksShortText(Model.Text, 10)

推荐答案

您可以通过定义一个定制 TextHelper 启动:

You could start by defining a custom TextHelper:

public class TextHelper
{
    public TextHelper(ViewContext viewContext)
    {
        ViewContext = viewContext;
    }

    public ViewContext ViewContext { get; private set; }
}

再有你的方法是这个扩展方法 TextHelper

public static class TextHelperExtensions
{
    public static IHtmlString MakeShortText(
        this TextHelper textHelper, 
        string text,
        int value
    )
    {
        ...
    }
}

然后,你可以自定义一个网页:

Then you could define a custom web page:

public abstract class MyWebViewPage<T> : WebViewPage<T>
{
    public override void InitHelpers()
    {
        base.InitHelpers();
        Text = new TextHelper(ViewContext);
    }

    public TextHelper Text { get; private set; }
}

然后在〜/查看/ web.config中不可以〜/的web.config )为您的意见剃刀用 pageBaseType 属性配置这个自定义网页为基础页:

then in your ~/Views/web.config (not in ~/web.config) configure this custom web page as base page for your Razor views using the pageBaseType attribute:

<pages pageBaseType="AppName.Web.Mvc.MyWebViewPage">
    <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
    </namespaces>
</pages>

,然后在你的意见,你将能够使用:

and then in your views you will be able to use:

@Text.MakeShortText(Model.Text, 10)

如果你想使用的语法如下所示,你的问题:

And if you wanted to use the following syntax as shown in your question:

@Text().MakeShortText(Model.Text, 10)

只需修改自定义视图引擎让文本不是属性,但将返回 TextHelper 。甚至返回的HtmlHelper 实例,这样你就不需要现有的扩展方法移至 TextHelper

simply modify the custom view engine so that Text is not a property but a method that will return an instance of the TextHelper. Or even return the HtmlHelper instance so that you don't need to move your existing extension methods to TextHelper:

public abstract class MyWebViewPage<T> : WebViewPage<T>
{
    public HtmlHelper Text()
    {
        return Html;
    }
}


第二个语法也是可能的:


The second syntax is also possible:

@Html.Text().MaksShortText(Model.Text, 10)

简单地定义一个定制的文本()扩展名为的HtmlHelper 类:

public static class HtmlExtensions
{
    public static TextHelper Text(this HtmlHelper htmlHelper)
    {
        return new TextHelper(htmlHelper.ViewContext);
    }
}

,然后以同样的方式在第一种情况下,你将有你的定制方法是这个扩展方法 TextHelper 类。

这篇关于创建视图中可用的自定义帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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