ASP.NET MVC 3 Razor - 为EditorFor添加类

use*_*348 184 asp.net-mvc razor asp.net-mvc-3

我正在尝试为输入添加一个类.

这不起作用:

@Html.EditorFor(x => x.Created, new { @class = "date" })
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 179

添加一个类Html.EditorFor在其模板中没有意义,你可以有许多不同的标签.所以你需要在编辑器模板中分配类:

@Html.EditorFor(x => x.Created)
Run Code Online (Sandbox Code Playgroud)

并在自定义模板中:

<div>
    @Html.TextBoxForModel(x => x.Created, new { @class = "date" })
</div>
Run Code Online (Sandbox Code Playgroud)

  • 这是GroundTtroller到ModelTom,我发送给你View,我以最MVC的方式浮动. (13认同)
  • 这很有效,谢谢.在我的模型中按照DisplayFormat注释格式化字段值时遇到问题.格式化为使用EditorFor和DisplayFor注释,但不是TextBoxFor :(看起来我必须使用自定义模板,除非有人能指出我缺少的东西.? (5认同)
  • 我有同样的问题,但我不能使用TextBoxFor,因为我需要DisplayFormat,它与编辑器或显示器一起使用,但同时我也需要类,以便我可以以只读形式显示文本 (3认同)

EF0*_*EF0 145

从ASP.NET MVC 5.1开始,可以添加一个类EditorFor(原始问题指定ASP.NET MVC 3,并且接受的答案仍然是最好的考虑).

@Html.EditorFor(x=> x.MyProperty,
    new { htmlAttributes = new { @class = "MyCssClass" } })
Run Code Online (Sandbox Code Playgroud)

请参阅:ASP.NET MVC 5.1中的新增功能,对编辑器模板的Bootstrap支持

  • 我爱你(只是一点点),它有效. (9认同)

Tuo*_*asK 101

您不能为通用EditorFor设置类.如果你知道你想要的编辑器,你可以立即使用它,在那里你可以设置类.您无需构建任何自定义模板.

@Html.TextBoxFor(x => x.Created, new { @class = "date" }) 
Run Code Online (Sandbox Code Playgroud)

  • 很划算,不需要构建自定义模板很好. (5认同)

prz*_*zno 37

您可以使用:

@Html.EditorFor(x => x.Created, new { htmlAttributes = new { @class = "date" } })
Run Code Online (Sandbox Code Playgroud)

(至少在ASP.NET MVC 5中,但我不知道ASP.NET MVC 3是怎么回事.)

  • 你请阅读我写的全部帖子,这是为了MVC5.这是谷歌的第一个结果,所以在我找到了我想要的解决方案之后,在这里分享它,也许它可以帮助一些人. (9认同)

big*_*mac 29

我有同样令人沮丧的问题,我不想创建一个应用于所有DateTime值的EditorTemplate(我的UI中有时候我想显示时间而不是jQuery UI下拉日历).在我的研究中,我遇到的根本问题是:

  • 标准的TextBoxFor助手允许我应用自定义类的"日期选择器"来渲染不显眼的jQuery UI日历,但TextBoxFor不会在没有时间的情况下格式化DateTime,因此导致日历渲染失败.
  • 标准的EditorFor会将DateTime显示为格式化字符串(当使用适当的属性修饰时[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")],但不允许我应用自定义的"日期选择器"类.

因此,我创建了自定义HtmlHelper类,它具有以下优点:

  • 该方法自动将DateTime转换为jQuery日历所需的ShortDateString(如果时间存在,jQuery将失败).
  • 默认情况下,帮助程序将应用所需的htmlAttributes来显示jQuery日历,但如果需要,可以覆盖它们.
  • 如果日期为null,则ASP.NET MVC将日期值为1/1/0001作为值.

此方法用空字符串替换它.

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());
}
Run Code Online (Sandbox Code Playgroud)

对于那些想要了解查找具有date-picker类装饰的对象的JQuery语法然后呈现日历的人,这里是:

$(document).ready(function () {
    $('.date-picker').datepicker({ inline: true, maxDate: 0, changeMonth: true, changeYear: true });
    $('.date-picker').datepicker('option', 'showAnim', 'slideDown');
});
Run Code Online (Sandbox Code Playgroud)


小智 15

可以通过AdditionalViewData提供类或其他信息 - 我使用它来允许用户基于数据库字段(propertyName,editorType和editorClass)创建表单.

根据您的初始示例:

@Html.EditorFor(x => x.Created, new { cssClass = "date" })
Run Code Online (Sandbox Code Playgroud)

并在自定义模板中:

<div>
    @Html.TextBoxFor(x => x.Created, new { @class = ViewData["cssClass"] })
</div>
Run Code Online (Sandbox Code Playgroud)

  • @ Html.EditorFor(x => x.Created,new {cssClass ="date"})对我不起作用 (8认同)
  • 这对我有用并且比标记的答案更好。它提供了使用模板的好处,同时允许进行一些自定义,而无需 jQuery hacks。 (2认同)

mar*_*ind 8

没有任何EditorFor覆盖允许您传入一个匿名对象,该对象的属性将以某种方式作为属性添加到某个标记上,尤其是对于内置编辑器模板.您需要编写自己的自定义编辑器模板,并将所需的值作为附加的viewdata传递.