2015-11-30 50 views
0

我已經在RazorEngine模板如下:RazorEngine - 如何使用我自己的HTML助手?

@using WAND.Helpers 
@model Tour.Entities.Accommodation 

<p> 
    You can view your WAND accommodation home page by @Html.HomepageActionLink("Accommodation","clicking here",@Model.AccommodationId) 
</p> 

的HomePageActionLink是一個幫助我寫的,看起來在某種程度上,這樣的:

public static MvcHtmlString HomepageActionLink(this HtmlHelper helper, string serviceProviderType, string linkText, int idValue) 
    { 
     ... 

     return new MvcHtmlString(link); 
    } 

的問題是,當我運行此,我收到一條錯誤消息:

More details about the error: - error: (64, 60) The name 'Html' does not exist in the current context 

有沒有辦法讓這個工作?

+1

哪條線給你錯誤? – DavidG

+0

你是否在視圖中添加了一個'using'語句給你的助手所在的程序集(或者在'web.config'文件中添加了程序集)? –

+0

@DavidG - 我打電話給Html.HomepageActionLink的行 –

回答

1

您可以用具有Html實現的類覆蓋BaseTemplateType。 Like

var config = new TemplateServiceConfiguration(); 
     // MvcHtmlStringFactory code from 
     // http://stackoverflow.com/questions/19431365/razorengine-html-helpers-work-but-escape-the-html 
     config.EncodedStringFactory = new MvcHtmlStringFactory(); 

     // HtmlTemplateBase code from 
     // http://stackoverflow.com/questions/8561164/razorengine-issues-with-html 
     config.BaseTemplateType = typeof(HtmlTemplateBase<>); 
     var service = RazorEngineService.Create(config); 

     var Colmodel ="@Html.BeginForm(\"UploadDocument\", \"Detail\", new{a=1,b=2})"; 

     string res = service.RunCompile(Colmodel, Colmodel, 
      model.GetType(), model); 
相關問題