ASP.NET MVC 3 Razor - Adding class to EditorFor

后端 未结 16 1855
长情又很酷
长情又很酷 2020-11-27 11:09

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

This is not working:

@Html.EditorFor(x => x.Created, new { @class = \"date\" })
相关标签:
16条回答
  • 2020-11-27 11:52

    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.

    0 讨论(0)
  • 2020-11-27 11:53

    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)
    
    0 讨论(0)
  • 2020-11-27 11:53

    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.

    CSS class declaration:

    .SpaceAvailableSearch input
    {
        width:25px;
    }
    

    View code:

    <span class="SpaceAvailableSearch">@Html.EditorFor(model => model.SearchForm.SpaceAvailable)</span>
    
    0 讨论(0)
  • 2020-11-27 11:54

    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.)

    0 讨论(0)
提交回复
热议问题