c# - 如何使用@Html.Raw、ASP.NET、Razor @Helper

标签 c# asp.net razor

我收到错误“NullReferenceException:...” 在@Html.Raw(...

听说是代码...

Commons.cshtml:

@helper BoxTitle(string CustomButtons)
{
    if (!string.IsNullOrEmpty(CustomButtons))
    {
        @Html.Raw(CustomButtons)
    }
}

View .cshtml:

<div class="box">
    @Commons.BoxTitle("<button class='sub-button'>New</button>")
    <div class="content">

    </div>
</div>

有人能解决这个问题吗?

最佳答案

默认的 HtmlHelpers 在 App_Code 中是不可访问的,这是我假设 Commons.cshtml 所在的位置。

您可以通过将使用 WebViewPage 作为参数并使用它来访问 HtmlHelpers 来稍微解决这个问题。您还需要为 System.Web.Mvc.Html 添加 @using 语句。

Commons.cshtml:

@using System.Web.Mvc.Html

@helper BoxTitle(System.Web.Mvc.WebViewPage wvp, string CustomButtons)
{
    if (!string.IsNullOrEmpty(CustomButtons))
    {
        @wvp.Html.Raw(CustomButtons)
    }
}

View .cshtml:

<div class="box">
    @Commons.BoxTitle(this, "<button class='sub-button'>New</button>")
    <div class="content">

    </div>
</div>

关于c# - 如何使用@Html.Raw、ASP.NET、Razor @Helper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26989243/

相关文章:

c# - 调试时在 Watch Window 中查看 Exception.Data

c# - 是否可以在没有“检测到文件修改”对话框的情况下从 VS 包写入当前 VS 解决方案文件

c# - ObjectDataSource 对注释掉的 GridView 有何 react ?

asp.net - 如何在asp.net 网站中使用wowza 媒体服务器?

c# - 简单更新查询不更新。没有异常(exception),没有错误。没什么

asp.net-mvc - 如何在 Razor 中获得选定的单选按钮

c# - 什么取代了 V3 Hot Tuna 中的 MvvmCross View 模型 PropertyChanged 事件?

c# - 使用 ASP.NET Framework 的简单 POST

c# - 浏览器关闭时停止调试 Visual Studio 2013

javascript - 在 mvc 4 razor 中逐一选中列表中的每个复选框后,如何调用 Ajax 函数