I\'m trying to add a class to an input.
This is not working:
@Html.EditorFor(x => x.Created, new { @class = \"date\" })
One Best way to apply class to @Html.EditorFor()
in MVC3 RAzor is
@Html.EditorFor(x => x.Created)
<style type="text/css">
#Created
{
background: #EEE linear-gradient(#EEE,#EEE);
border: 1px solid #adadad;
width:90%;
}
</style>
It will add above style to your EditorFor()
It works for MVC3.
Using jQuery, you can do it easily:
$("input").addClass("class-name")
Here is your input tag
@Html.EditorFor(model => model.Name)
For DropDownlist you can use this one:
$("select").addClass("class-name")
For Dropdownlist:
@Html.DropDownlistFor(model=>model.Name)
I just needed to set the size of one textbox on one page. Coding attributes on the model and creating custom editor templates were overkill, so I just wrapped the @Html.EditorFor call with a span tag that called a class which specifies the size of the textbox.
.SpaceAvailableSearch input
{
width:25px;
}
<span class="SpaceAvailableSearch">@Html.EditorFor(model => model.SearchForm.SpaceAvailable)</span>
You can use:
@Html.EditorFor(x => x.Created, new { htmlAttributes = new { @class = "date" } })
(At least with ASP.NET MVC 5, but I do not know how that was with ASP.NET MVC 3.)