开发者

Can't use differrent EditorTemplates for the same DateTime value?

Let's say in my model I have public DateTime Date {get;set;} property. On the view it should be represented in a couple of textboxes - one for the Date and another for the Time.

I created two different editor templates

DateTime.cshtml:

@model DateTime?
@H开发者_JAVA技巧tml.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker", style = "width:90px"  })

and TimeOnly.cshtml

@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortTimeString() : string.Empty), new { @class = "timePicker", style = "width:90px" })

And the EditorFor fields on the View

@Html.EditorFor(x => x.Date)
@Html.EditorFor(x => x.Date,"TimeOnly")

Now, the problem is that the actual value of Date property passed to the controller comes with default time 12:00. The same thing happens to the Date if we change the order of the EditorFor fields - the Date rolls back to default Date value immediately after TimeOnly template has applied, although the Time part of it stays correct.

Again on the view itself the values are ok, but on submit it passes default value to the controller, either with correct Date and default Time and vice versa (it depends which template used first)

Of course I can always separate Date and Time just by adding another property to the model, but is there a better solution?


Scott Hanselman blogged about how a custom model binder could be used in this case.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜