开发者

MVC3 Custom EditorFor DateTime and DisplayFormat attribute

开发者 https://www.devze.com 2023-04-12 06:21 出处:网络
I\'ve created a custom EditorFor DateTime and added a classname to the textbox @model DateTime @Html.TextBoxFor(m 开发者_如何学C=> m, new {@class = \"date-time-picker\" })

I've created a custom EditorFor DateTime and added a classname to the textbox

@model DateTime
@Html.TextBoxFor(m 开发者_如何学C=> m, new {  @class = "date-time-picker" })

So that I can connect jquery datepicker to it client side

$("input.date-time-picker").datetimepicker();

The problem is that for the

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:g}")]

attribute to work you need to use the built in EditorFor (Or extracting the model metadata and do the same stuff yourself), so whats the best way of overriding the default EditorFor, add the classname and call the built in editorfor so that I can use the DisplayFormat attribute


(Question answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

I solved it by doing this, but it feels like reinventing the wheel

@model DateTime  
@Html.TextBox("", string.Format(ModelMetadata.FromLambdaExpression(x => x, ViewData).DisplayFormatString, Model), new { @class = "date-time-picker" })
0

精彩评论

暂无评论...
验证码 换一张
取 消