2015-11-30 17 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); 
相关问题