@html.textboxfor 添加属性,Html.TextBoxFor formatting or Html.EditorFor htmlAttributes?

Use @Html.EditorFor(m => m.DateModified), because otherwise the DisplayFormat attribute will have no effect.

To add further attributes like a CSS class, you have to create an editor template for the DateTime.

Create a file EditorTemplates/DateTime.cshtml with the following content:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new

{

@class="date"

})

Please note that the value of the TextBox is not set with the Model directly, but rather with the TemplateInfo.FormattedModelValue, because that value will be formatted according to the DisplayFormat attribute while the Model not. (This took me quite some time to realize. :))

In simple cases this might be enough, e.g. if the CSS class can be the same for all date editors.

If you want to parametrize the attribute, you can do that as well, passing the attribute value parameter to the EditorFor.

@Html.EditorFor(m => m.DateModified, new { @class = "someClass" })

However, this parameter will be not automagically delegated to the HTML control as attribute, but you have to "handle it" in the template explicitly. According to my experiences you can access this parameter value in the ViewData in the template, so the parametrized template looks like this:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new

{

@class=ViewData["class"]

})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值