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?
An HTML Helper is just a method that returns a HTML string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input>, <button> and <img> tags etc.
In MVC, HTML Helper can be considered as a method that returns you a string. This string can describe the specific type of detail of your requirement. Example: We can utilize the HTML Helpers to perform standard HTML tags, for example HTML<input>, and any <img> tags.
Tag Helpers are attached to HTML elements inside your Razor views and can help you write markup that is both cleaner and easier to read than the traditional HTML Helpers. HTML Helpers, on the other hand, are invoked as methods that are mixed with HTML inside your Razor views.
No, but you can use the standard HTML Helpers which do a lot of the same things. And ASP.NET (non-code) MVC is absolutely not "legacy", it's still fully supported with no roadmap to end that, and so is WebForms for that matter.
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();
}
Declarative helpers in Razor are static methods. You could pass the Html helper as argument:
@helper DisplayForm(HtmlHelper html) {
@html.EditorForModel();
}
@DisplayForm(Html)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With