ASP.NET MVC 3 Razor - Adding class to EditorFor

后端 未结 16 1853
长情又很酷
长情又很酷 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:32

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

    I had the same frustrating issue, and I didn't want to create an EditorTemplate that applied to all DateTime values (there were times in my UI where I wanted to display the time and not a jQuery UI drop-down calendar). In my research, the root issues I came across were:

    • The standard TextBoxFor helper allowed me to apply a custom class of "date-picker" to render the unobtrusive jQuery UI calender, but TextBoxFor wouldn't format a DateTime without the time, therefore causing the calendar rendering to fail.
    • The standard EditorFor would display the DateTime as a formatted string (when decorated with the proper attributes such as [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")], but it wouldn't allow me to apply the custom "date-picker" class.

    Therefore, I created custom HtmlHelper class that has the following benefits:

    • The method automatically converts the DateTime into the ShortDateString needed by the jQuery calendar (jQuery will fail if the time is present).
    • By default, the helper will apply the required htmlAttributes to display a jQuery calendar, but they can be overridden if needs be.
    • If the date is null, ASP.NET MVC will put a date of 1/1/0001 as a value.

    This method replaces that with an empty string.

    public static MvcHtmlString CalenderTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
    {
        var mvcHtmlString = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, htmlAttributes ?? new { @class = "text-box single-line date-picker" });
        var xDoc = XDocument.Parse(mvcHtmlString.ToHtmlString());
        var xElement = xDoc.Element("input");
        if (xElement != null)
        {
            var valueAttribute = xElement.Attribute("value");
            if (valueAttribute != null)
            {
                valueAttribute.Value = DateTime.Parse(valueAttribute.Value).ToShortDateString();
                if (valueAttribute.Value == "1/1/0001")
                    valueAttribute.Value = string.Empty;
            }
        }
        return new MvcHtmlString(xDoc.ToString());
    }
    

    And for those that want to know the JQuery syntax that looks for objects with the date-picker class decoration to then render the calendar, here it is:

    $(document).ready(function () {
        $('.date-picker').datepicker({ inline: true, maxDate: 0, changeMonth: true, changeYear: true });
        $('.date-picker').datepicker('option', 'showAnim', 'slideDown');
    });
    
    0 讨论(0)
  • 2020-11-27 11:34

    There isn't any EditorFor override that lets you pass in an anonymous object whose properties would somehow get added as attributes on some tag, especially for the built-in editor templates. You would need to write your own custom editor template and pass the value you want as additional viewdata.

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

    Adding a class to Html.EditorFor doesn't make sense as inside its template you could have many different tags. So you need to assign the class inside the editor template:

    @Html.EditorFor(x => x.Created)
    

    and in the custom template:

    <div>
        @Html.TextBoxForModel(x => x.Created, new { @class = "date" })
    </div>
    
    0 讨论(0)
  • 2020-11-27 11:39

    No need to create custom template for MVC 4. Use TextBox instead of EditFor here special html attributes are not supported, it is only supported from MVC 5. TextBox is should support for MVC 4, I don't know about other version.

    @Html.TextBox("test", "", new { @id = "signupform", @class = "form-control", @role = "form",@placeholder="Name" })
    
    0 讨论(0)
  • 2020-11-27 11:40
    @Html.EditorFor(m=>m.Created ...) 
    

    does not allow any arguments to be passed in for the Text box

    This is how you can apply attributes.

    @Html.TextBoxFor(m=>m.Created, new { @class= "date", Name ="myName", id="myId" })
    
    0 讨论(0)
提交回复
热议问题