asp.net mvc - How to use built-in HTML helpers within a custom HTML helper? -
i'm trying use beginform in helper razor function eg.
@helper modal(string name) { <div id="@name"> @using (html.beginform("add", "user", formmethod.post)) { <fieldset> @html.label("name") @html.textbox("name", new { @class = "text ui-widget-content ui-corner-all" }) @html.label("email") @html.textbox("email", new { @class = "text ui-widget-content ui-corner-all" }) <input type="submit" tabindex="-1" style="position:absolute; top:-1000px"> </fieldset> } </div> } however getting error:
@myhelper.modal("dialog-form") it's due html... markup, without it works fine html.
what missing make working?
i have added @using system.web.mvc.html; still not recognise formmethod.
unfortunately declarative helpers defined in app_code folder seems inherit system.web.webpages.helperpage instead of system.web.mvc.webviewpage normal cshtml files inherit.
it seems helper page has html property it's null.
however, seems can access helpers through pagecontext.page. you'll need add using statements (all namespaces web.config located in views folder) can access important extensions methods html.beginform.
here sample demo code:
@using system.web.mvc @using system.web.mvc.routing @using system.web.mvc.html @using system.web.mvc.ajax @using system.web.mvc.razor @using system.web.optimization @helper mycustomhelper() { var wvp = pagecontext.page system.web.mvc.webviewpage; var html = wvp.html; var ajax = wvp.ajax; var url = wvp.url; var viewbag = wvp.viewbag; // ... helper code goes here ... @using (html.beginform("add", "user", formmethod.post)) @ajax.beginform ... @url.action ... // ... } hope helps.
Comments
Post a Comment