Using html helpers in Razor web helper

前提是你 提交于 2019-12-18 13:08:15

问题


I am trying to create a Razor web helper something like this :

@helper DisplayForm() {    
    @Html.EditorForModel();    
}

But this gives the error "CS0103: The name 'Html' does not exist in the current context".

Is there any way to reference html helpers within web helpers?


回答1:


You can cast the static Page property from the context to the correct type:

@helper MyHelper() {
    var Html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html;

    Html.RenderPartial("WhatEver");
    @Html.EditorForModel();
}



回答2:


Declarative helpers in Razor are static methods. You could pass the Html helper as argument:

@helper DisplayForm(HtmlHelper html) {
    @html.EditorForModel(); 
}

@DisplayForm(Html)



回答3:


Razor inline WebHelper is generate static method.

So can not access instance member.

@helper DisplayForm(HtmlHelper html){
    @html.DisplayForModel()
}

How about this?



来源:https://stackoverflow.com/questions/4032490/using-html-helpers-in-razor-web-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!