ASP.NET MVC 3 Razor - Adding class to EditorFor

2020-01-25 04:25发布

I'm trying to add a class to an input.

This is not working:

@Html.EditorFor(x => x.Created, new { @class = "date" })

16条回答
爷、活的狠高调
2楼-- · 2020-01-25 04:57

You could also do it via jQuery:

$('#x_Created').addClass(date);
查看更多
走好不送
3楼-- · 2020-01-25 04:57

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.

查看更多
Explosion°爆炸
4楼-- · 2020-01-25 04:58

It is possible to provide a class or other information through AdditionalViewData - I use this where I'm allowing a user to create a form based on database fields (propertyName, editorType, and editorClass).

Based on your initial example:

@Html.EditorFor(x => x.Created, new { cssClass = "date" })

and in the custom template:

<div>
    @Html.TextBoxFor(x => x.Created, new { @class = ViewData["cssClass"] })
</div>
查看更多
爷、活的狠高调
5楼-- · 2020-01-25 04:58

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)
查看更多
登录 后发表回答