Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add HTML attributes to a Razor HtmlHelper?

I have a general question about some Razor Syntax I keep finding in a project I am working on...

One of the main helpers being used is: @Html.InputFor

They then insert some Lambda for example: @Html.InputFor(_ => _.User)

My question is, how do I work with this helper (I couldn't find any details via a google search) i.e adding attributes like custom css classes?

Is there a better helper I should be using? (I am new to Razor)

like image 528
Matt D. Webb Avatar asked May 09 '14 09:05

Matt D. Webb


1 Answers

I think you mean @Html.TextBoxFor. That extension method can be found in the InputExtensions class.

How to attach an attribute to it? Use the htmlAttributes property:

@Html.TextBoxFor(x => x.User, htmlAttributes : new { @class = "cssclass" } )
like image 124
Patrick Hofman Avatar answered Oct 20 '22 00:10

Patrick Hofman