【问题标题】:Using @Html inside shared @helper in App_Code在 App_Code 的共享 @helper 中使用 @Html
【发布时间】:2011-07-13 13:23:29
【问题描述】:

我在学习时正在构建一个非常基本的 MVC3 站点,但在使用以下声明性 Razor html 帮助程序时遇到了困难。

在App_Code文件夹内的RMB.cshtml中:

@helper ReplaceCrLf(string strText)
{
    @Html.Raw(Html.Encode(strText).Replace(Environment.NewLine, "<br />"));
}

在我的 index.cshtml 视图中:

@RMB.ReplaceCrLf(Model.Post)

这给了我一个关于 Html 的空引用异常,因为它似乎不知道它是什么。我可以通过将 Html 从视图传递给助手来解决这个问题,但我想知道是否有另一种方法让我的共享 html 助手能够引用 Html,而无需将其传递给我编写的任何助手?

为了完整起见,这里是工作解决方法:

在 App_Code 中的 RMB.cshtml 中

@helper ReplaceCrLf(string strText, System.Web.Mvc.HtmlHelper Html)
{
    @Html.Raw(Html.Encode(strText).Replace(Environment.NewLine, "<br />"));
}

在 index.cshtml 视图中

@RMB.ReplaceCrLf(Model.Post, Html)

【问题讨论】:

  • 基于这个answer,目前将自定义Helper放入AppCode文件夹是一个限制。

标签: asp.net-mvc asp.net-mvc-3 razor razor-declarative-helpers


【解决方案1】:

我现在将它添加到 App_Code 中的任何 .cshtml 文件中。

// Using's are needed to ensure helpers function correctly.
@using System.Web.Mvc;
@using System.Web.Mvc.Html;
@using System.Web.Mvc.Routing;
@using System.Web.Mvc.Razor;
@functions {
    private static WebViewPage page { get { return PageContext.Page as WebViewPage; } } 
    private static System.Web.Mvc.HtmlHelper<dynamic> html { get { return page.Html; } }
    private static UrlHelper url { get { return page.Url; } }
    private static dynamic viewBag { get { return page.ViewBag; } }
}

编辑:已将助手变量名称修改为小写,因为我与内置助手名称有一些冲突。我已将 HTML 帮助器修改为通用的,允许使用 TextBoxFor 等帮助器

这使得文件中的所有@helper 方法和函数都可以使用这些出色的助手。

非常感谢 Neshta 的原创概念!


回答问题的完整示例:

在App_Code文件夹下的RMB.cshtml中

@functions {
    public static WebViewPage page = (PageContext.Page as WebViewPage);
    public static HtmlHelper<object> html = page.Html;
}

@helper ReplaceCrLf(string strText)
{
    @html.Raw(html.Encode(strText).Replace(Environment.NewLine, "<br />"));
}

在视图中:

@RMB.ReplaceCrLf("line1\nline2") // No passing HtmlHelper

【讨论】:

  • @Omid-RH 我拒绝了编辑,因为我相信我明确地将这个命名空间放在 HtmlHelper 上,因为包含在之前的 using 语句。如果您可以确认不是这种情况,欢迎您再次提出修改建议,我会接受。
【解决方案2】:

我使用了类似以下的解决方法

@helper HtmlRaw(string s)
{
    @(new HtmlString(s))
}

@helper ReplaceCrLf(string strText)
{
    @HtmlRaw(HttpUtility
                        .HtmlEncode(strText)
                        .Replace(Environment.NewLine, "<br />"));
}

【讨论】:

    【解决方案3】:

    可以从 PageContext 中获取助手:

    var page = (WebViewPage) PageContext.Page;
    var Url = page.Url;
    var Html = page.Html;
    var ViewBag = page.ViewBag;
    // and so on
    

    用法(如标准助手):

    @Url.Action("Index", "Home")
    @Html.Raw("some encoded string")
    

    【讨论】:

      【解决方案4】:

      看起来像 App_Code 中的助手工作,但您无权访问标准 MVC Html。帮手

      Razor: Declarative HTML helpers

      【讨论】:

      【解决方案5】:

      在你的助手中添加第二个参数,如下所示

      @helper ReplaceCrLf(string strText,HtmlHelper<dynamic> html)
      {
          @Html.Raw(html.Encode(strText).Replace(Environment.NewLine, "<br />"));
      }
      

      当你使用这个助手时,传递 this.Html 作为第二个参数

      【讨论】:

        【解决方案6】:

        这就够了:

        var message = new HtmlString(toast.Message.Replace(Environment.NewLine, "<br />"))
        

        无需使用Html.Raw().

        【讨论】:

        • 确认,在 CSHTML 中做对了 @(new HtmlString(ShortenedText))
        猜你喜欢
        • 1970-01-01
        • 2011-06-19
        • 1970-01-01
        • 2015-09-30
        • 1970-01-01
        • 1970-01-01
        • 2012-08-29
        • 1970-01-01
        • 2019-01-21
        相关资源
        最近更新 更多