Brian Mains
Brian Mains

Reputation: 50728

Razor support of generic extension methods

With regards to the Razor view engine, say I want to render Html.TextBoxFor<SomeModel>(i => i.Name), it doesn't seem that the inline syntax works as in:

@Html.TextBoxFor<SomeModel>(i => i.Name)

This doesn't seem to work because it interprets the generic as an HTML tag. I could use a code-block approach, but then what's the best approach to output the content? The HTML string returned from this method, do I response.write it, or is there a syntax for it, or what's the approach?

Thanks.

Upvotes: 8

Views: 2092

Answers (2)

amelvin
amelvin

Reputation: 9051

There are four ways that I've found to get razor to explicitly parse (as opposed to trying to work out what to do):

 - @(some code)  (this is the method used by @Matt Hamilton) 
 - Html.Raw("some encoded text") 
 - <text>Some encoded text</text>
 - @@

Only the first of these would work here.

There is a walkthrough on PluralSight in the Razor and ASP.NET MVC 3.0 | Intermingling code and markup section, on this exact subject.

Upvotes: 6

Matt Hamilton
Matt Hamilton

Reputation: 204139

How about:

@(Html.TextBoxFor<SomeModel>(i => i.Name))

Do parentheses help?

Upvotes: 9

Related Questions