Adding style to Editor For

前端 未结 5 1078
独厮守ぢ
独厮守ぢ 2020-12-18 21:48

I\'m trying to apply a Style to the Editor for an element, but I can\'t make it work; what am I doing wrong?

@Html.EditorFor(model => model.ClienteNuevo)
         


        
相关标签:
5条回答
  • 2020-12-18 22:15
    @Html.EditorFor(model => model.ClienteNuevo)
    @Html.ValidationMessageFor(model => model.ClienteNuevo, "", new { @class = "yourclass" })
    

    I hope you help

    0 讨论(0)
  • 2020-12-18 22:16

    EditorFor does not allow for styling as there are no parameters for additional attributes. The reason for this is because the EditorFor doesn't always generate a single element as it can be overridden. To style a specific type of element you need to use the specific editor you want to use. For instance if the editor is a textbox just use TextBoxFor and apply the styling that way.

    0 讨论(0)
  • 2020-12-18 22:17

    EditorFor invokes template views rather than outputting a fixed element, so it doesn't take html attributes as an argument. For something like what you're doing the easiest workaround would be to surround the editor and validation message with another element, and apply the style to that instead:

    <div style="width: 500px;">
    @Html.EditorFor(model => model.ClienteNuevo)
    @Html.ValidationMessageFor(model => model.ClienteNuevo,"")
    </div>
    
    0 讨论(0)
  • 2020-12-18 22:24

    You can create own css class and appendd it to your editor:

    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "custom-editor" } })
    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
    
    0 讨论(0)
  • 2020-12-18 22:32

    Since MVC 5.1, you can pass in custom attributes with using the htmlAttributes as a key:

    @Html.EditorFor(model => model.ClienteNuevo, 
        new { htmlAttributes = new { @class = "form-control" } })
    

    In older MVC versions there is no way to add html attributes with the EditorFor method.
    You should create a custom editor template or use Html.TextboxFor istead of EditorFor. You should check these topics topic1, topic2.

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