使用@Html在APP_ code共享@helper内 [英] Using @Html inside shared @helper in App_Code
问题描述
我建立一个非常基本的MVC3的网站,而我学习,我有以下声明剃刀HTML辅助困难。
里面RMB.cshtml内APP_ $ C $文件夹C:
@helper ReplaceCrLf(字符串strText中)
{
@ Html.Raw(Html.En code(strText的).Replace(Environment.NewLine,< BR />中));
}
在我index.cshtml观点:
@ RMB.ReplaceCrLf(Model.Post)
这让我在助手的HTML一个空引用异常,因为似乎知道它是什么它没有。我可以解决此通过从视图助手通过HTML,但是我不知道是否有另一种方式为我的共享HTML帮助者能够引用的Html没有我不得不给它传递给曾经帮助我写?
有关完整,这里是工作的解决方法:
在RMB.cshtml在APP_ code
@helper ReplaceCrLf(字符串strText中,System.Web.Mvc.HtmlHelper的Html)
{
@ Html.Raw(Html.En code(strText的).Replace(Environment.NewLine,< BR />中));
}
在index.cshtml视图
@ RMB.ReplaceCrLf(Model.Post,HTML)
我补充这在现在APP_ code任何.cshtml文件。
需要 //使用的保证助手正常工作。
@using System.Web.Mvc;
@using System.Web.Mvc.Html;
@using System.Web.Mvc.Routing;
@using System.Web.Mvc.Razor;
@功能 {
私有静态页面WebViewPage {{返回PageContext.Page为WebViewPage; }}
私有静态System.Web.Mvc.HtmlHelper的Html {{返回page.html即可; }}
私有静态UrlHelper网址{{返回page.Url; }}
私有静态动态ViewBag {{返回page.ViewBag; }}
}
这使得那些提供精彩的助手文件中的所有@helper方法和功能。
非常感谢 Neshta A>的原始概念!
完整的例子来回答问题:
在RMB.cshtml在APP_ code文件夹
@functions {
公共静态WebViewPage页=(PageContext.Page为WebViewPage);
公共静态的HtmlHelper<对象> HTML = page.html即可;
}@helper ReplaceCrLf(字符串strText中)
{
@ Html.Raw(Html.En code(strText的).Replace(Environment.NewLine,< BR />中));
}
在View:
@ RMB.ReplaceCrLf(行1 \\ nline2)//没有传递的HtmlHelper
I am building a very basic MVC3 site while I learn and I am having difficulty with the following declarative Razor html helper.
Inside RMB.cshtml inside App_Code folder:
@helper ReplaceCrLf(string strText)
{
@Html.Raw(Html.Encode(strText).Replace(Environment.NewLine, "<br />"));
}
Inside my index.cshtml view:
@RMB.ReplaceCrLf(Model.Post)
This gives me a null reference exception on Html in the helper, because it doesn't seem to know what it is. I can work around this by passing Html from the view to the helper, but I was wondering if there is another way for my shared html helpers to be able to reference Html without me having to pass it in to ever helper I write?
For completeness, here is the working workaround:
In RMB.cshtml in App_Code
@helper ReplaceCrLf(string strText, System.Web.Mvc.HtmlHelper Html)
{
@Html.Raw(Html.Encode(strText).Replace(Environment.NewLine, "<br />"));
}
In index.cshtml view
@RMB.ReplaceCrLf(Model.Post, Html)
I add this to any .cshtml files in App_Code now.
// 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 Html { get { return page.Html; } }
private static UrlHelper Url { get { return page.Url; } }
private static dynamic ViewBag { get { return page.ViewBag; } }
}
This makes those wonderful helpers available to all @helper methods and functions in the file.
Many thanks to Neshta for the original concept!
Complete Example to answer question:
In RMB.cshtml in the App_Code folder
@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 />"));
}
In View:
@RMB.ReplaceCrLf("line1\nline2") // No passing HtmlHelper
这篇关于使用@Html在APP_ code共享@helper内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!