views:

63

answers:

2

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  A: 

Razor inline WebHelper is generate static method.

So can not access instance member.

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

How about this?

takepara
+2  A: 

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

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

@DisplayForm(Html)
Darin Dimitrov
When I try this I get the error "CS1061: 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'EditorForModel' and no extension method 'EditorForModel' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)"
Craig
That's because your view needs to be strongly typed: `@model MyNs.Models.FooViewModel`.
Darin Dimitrov