Html.TextBoxFor格式或Html.EditorFor htmlAttributes? [英] Html.TextBoxFor formatting or Html.EditorFor htmlAttributes?
问题描述
我有点难倒,因为我想格式化值,并添加HTML属性,CSS类。
I am kind of stumped because, I want to format the value and add a html attribute for css class.
如果我用 @ Html.TextBoxFor(M = GT; m.DateModified)
- 我可以添加HTML属性,但格式不会通过成员DisplayFormat属性工作
If I use @Html.TextBoxFor(m => m.DateModified)
- I can add html attribute but formatting does not work via DisplayFormat attribute on the member.
如果我用 @ Html.EditorFor(M = GT; m.DateModified)
- 格式化的作品,但我不能添加HTML属性
If I use @Html.EditorFor(m => m.DateModified)
- Formatting works but I cannot add html attribute
如果我用 @ Html.TextBox(DateModified,Model.DateModified,...)
- 我得到空引用异常时,型号为空时,形式是在添加模式
If I use @Html.TextBox("DateModified", Model.DateModified, ...)
- I get null reference exception when Model is null when the form is in add mode
什么是实现这一目标的最佳途径?
What is the best way to achieve this?
推荐答案
我结束了创建我的日期选择器自定义编辑器模板,所以解决这个:
I ended up solving this by creating a custom editor template for my date picker as so:
共享/ EditorTemplates / DateTime.cshtml
Shared/EditorTemplates/DateTime.cshtml
@model System.DateTime?
@Html.TextBox("", Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") : string.Empty, new { @class = "date-picker" })
然后在我原来的页面继续使用
Then in my original page continue to use
@Html.EditorFor(m => m.DateModified)
这篇关于Html.TextBoxFor格式或Html.EditorFor htmlAttributes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!