2011-07-13 107 views
27

我正在構建一個非常基本的MVC3網站,同時學習了以下聲明式Razor html助手。在App_Code中使用共享@helper中的@Html

內RMB.cshtml App_Code文件夾內:

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

裏面我index.cshtml觀點:

@RMB.ReplaceCrLf(Model.Post) 

這給我的助手HTML的一個空引用異常,因爲它不」 t似乎知道它是什麼。我可以通過將Html從視圖傳遞給幫助程序來解決這個問題,但是我想知道是否有另一種方式讓我的共享html幫助程序能夠引用Html,而無需將它傳遞給我寫的幫助程序?

爲了完整起見,這裏的工作解決方法:

在RMB.cshtml在App_Code文件

@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) 
+0

: //stackoverflow.com/questions/4451287/razor-declarative-html-helpers/4453637#4453637),它目前是在AppCode中放置自定義助手的限制文件夾中。 –

回答

29

我添加此到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的原始概念!


完整的例子來回答問題:

在RMB.cshtml App_Code文件夾中

@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 />")); 
} 

在View:基於該[答案](HTTP

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

貌似App_Code文件的工作,但你的幫手無法訪問標準的MVC Html。助手

看到Razor: Declarative HTML helpers

+0

補充一下,這也最終把我帶到原來的博客,我首先看到了如何做到這一點。這裏的期待MVC4然後:( – Treborbob

+2

您好我已經創建了ASP.NET用戶語音的想法與此有關:![對意見的app_code支持@helper ExtensionMethod(此的HtmlHelper HTML)](HTTP://aspnet.uservoice。 com/forums/41201-asp-net-mvc/suggestions/3670180-support-helper-extensionmethod-this-htmlhelper-ht)...感謝! –

20

我用一種變通方法像下面

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

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

這個是100%的黃金 – pimbrouwers

+0

好的解決辦法! – Kosmog

2

放在你的助手添加第二個參數爲波紋管

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

當你使用這個幫手通過this.Html作爲第二參數

+0

迄今爲止最好的答案,謝謝! – WhatIsHeDoing

1

這應該夠了:

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

無需使用Html.Raw().

+1

證實,這樣做是正確的CSHTML @(新HtmlString(ShortenedText)) SushiGuy

13

它有可能獲得從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")