Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a generic @helper method with Razor?

I am trying to write a helper in Razor that looks like the following:

@helper DoSomething<T, U>(Expression<Func<T, U>> expr) where T : class 

Unfortunately, the parser thinks that <T is the beginning of an HTML element and I end up with a syntax error. Is it possible to create a helper with Razor that is a generic method? If so, what is the syntax?

like image 802
mkedobbs Avatar asked Jan 21 '11 15:01

mkedobbs


People also ask

Does ASP NET MVC use razor?

Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML. It's just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.

Which HtmlHelper object method should you use?

It binds the model object to HTML controls to display the value of model properties into those controls and also assigns the value of the controls to the model properties while submitting a web form. So always use the HtmlHelper class in razor view instead of writing HTML tags manually.

What is Razor syntax in MVC?

Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write a mix of HTML and server-side code using C# or Visual Basic. Razor view with visual basic syntax has .

What is the Razor syntax in dotnet?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML.


2 Answers

This is possible to achieve inside a helper file with the @functions syntax but if you want the razor-style readability you are referring to you will also need to call a regular helper to do the HTML fit and finish.

Note that functions in a Helper file are static so you would still need to pass in the HtmlHelper instance from the page if you were intending to use its methods.

e.g. 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> } ... 
like image 121
chrismilleruk Avatar answered Sep 22 '22 15:09

chrismilleruk


No, this is not currently possible. You could write a normal HTML helper instead.

public static MvcHtmlString DoSomething<T, U>(     this HtmlHelper htmlHelper,      Expression<Func<T, U>> expr ) where T : class {     ... } 

and then:

@(Html.DoSomething<SomeModel, string>(x => x.SomeProperty)) 

or if you are targeting the model as first generic argument:

public static MvcHtmlString DoSomething<TModel, TProperty>(     this HtmlHelper<TModel> htmlHelper,      Expression<Func<TModel, TProperty>> expr ) where TModel : class {     ... } 

which will allow you to invoke it like this (assuming of course that your view is strongly typed, but that's a safe assumption because all views should be strongly typed anyways :-)):

@Html.DoSomething(x => x.SomeProperty) 
like image 31
Darin Dimitrov Avatar answered Sep 20 '22 15:09

Darin Dimitrov