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)
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支持
Tuo*_*asK 101
您不能为通用EditorFor设置类.如果你知道你想要的编辑器,你可以立即使用它,在那里你可以设置类.您无需构建任何自定义模板.
@Html.TextBoxFor(x => x.Created, new { @class = "date" })
Run Code Online (Sandbox Code Playgroud)
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是怎么回事.)
big*_*mac 29
我有同样令人沮丧的问题,我不想创建一个应用于所有DateTime值的EditorTemplate(我的UI中有时候我想显示时间而不是jQuery UI下拉日历).在我的研究中,我遇到的根本问题是:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")],但不允许我应用自定义的"日期选择器"类.因此,我创建了自定义HtmlHelper类,它具有以下优点:
此方法用空字符串替换它.
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)
没有任何EditorFor覆盖允许您传入一个匿名对象,该对象的属性将以某种方式作为属性添加到某个标记上,尤其是对于内置编辑器模板.您需要编写自己的自定义编辑器模板,并将所需的值作为附加的viewdata传递.
| 归档时间: |
|
| 查看次数: |
198002 次 |
| 最近记录: |