This can be implemented inside a helper file with the @functions syntax, but if you want razor-style readability to reference you, you will also need to call a regular helper to make the HTML fit and finish.
Note that the functions in the Helper file are static, so you still need to pass the HtmlHelper instance from the page if you intend to use its methods.
eg. Views \ MyView.cshtml:
@MyHelper.DoSomething(Html, m=>m.Property1) @MyHelper.DoSomething(Html, m=>m.Property2) @MyHelper.DoSomething(Html, m=>m.Property3)
App_Code \ MyHelper.cshtml:
@using System.Web.Mvc; @using System.Web.Mvc.Html; @using System.Linq.Expressions; @functions { public static HelperResult DoSomething<TModel, TItem>(HtmlHelper<TModel> html, Expression<Func<TModel, TItem>> expr) { return TheThingToDo(html.LabelFor(expr), html.EditorFor(expr), html.ValidationMessageFor(expr)); } } @helper TheThingToDo(MvcHtmlString label, MvcHtmlString textbox, MvcHtmlString validationMessage) { <p> @label <br /> @textbox @validationMessage </p> } ...
chrismilleruk Mar 12 '11 at 10:11 2011-03-12 22:11
source share