【问题标题】:Html standard helper is null from within custom helperHtml 标准助手在自定义助手中为空
【发布时间】:2015-05-29 06:39:55
【问题描述】:

我在 .cshtml 助手中使用 @helper 制作了一些自定义助手,如下所示:

@helper MyCustomField(string value, bool valueAsRaw)
{
    <div>
        @if (valueAsRaw)
        {
            @Html.Raw(value)
        }
        else
        {
            @value
        }
    </div>
}

当我运行它时,我在 @Html 帮助器对象上得到空值异常。如何访问标准 HtmlHelper 方法?

在 VS 中,我在 Html.Raw 上获得了智能感知,因此它可用,但在运行时为空。

【问题讨论】:

    标签: asp.net-mvc html-helper


    【解决方案1】:

    可能是您错误地访问了辅助方法。 使用了您的代码,它按预期工作。

    查看.cshtml

    <table>
    <tr>
    <td>
      @MyCustomFiled("StackOverFlow",true)
    </td>
    </tr>
    </table>
    
    @helper MyCustomFiled(string value, bool valueAsRaw)
    {
        <div>
            @if (valueAsRaw)
            {
                @Html.Raw(value)
            }
            else
            {
                @value
            }
        </div>
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-07
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      相关资源
      最近更新 更多