在 ASP.NET MVC 中将 DateTime 拆分为日期和时间
我已阅读 Scott Hanselman 的关于此主题的帖子。我发现 另一个文章 这似乎是一种更简单的方法。我决定尝试第二个。
但是,创建 EditorTemplates 的第一部分对我来说不起作用。我复制了 DateTime.ascx 和 TimeSpan.ascx 是作者写的。然后我在我的视图中分割字段。
<div class="editor-label">
<%= Html.LabelFor(model => model.LeaveRequest.DateOfLeave)%>
</div>
<div class="editor-field">
<div class="date-container">
<%= Html.TextBoxFor(model => model.LeaveRequest.DateOfLeave.Date)%>
</div>
<div class="time-container">
<%= Html.TextBoxFor(model => model.LeaveRequest.DateOfLeave.TimeOfDay)%>
</div>
<div class="clear">
<%= Html.ValidationMessageFor(model => model.LeaveRequest.DateOfLeave)%>
</div>
</div>
我遇到的问题是我得到的行为与作者解释的应该发生的行为不同。这是我的结果的屏幕截图。
我一定错过了一些东西,但我不知道是什么。我已经读了这篇文章好几遍了,但看不出我错过了什么。我猜想需要有一些东西来告诉程序我们新的 EditorTemplates,但我不知道如何做。
I have read through Scott Hanselman's post on this topic. I found another article that seems to be a simpler approach. The second of which I decided to try.
However, the first portion in creating the EditorTemplates is not working for me. I copied DateTime.ascx and TimeSpan.ascx has the author had them written. Then I split the fields in my view.
<div class="editor-label">
<%= Html.LabelFor(model => model.LeaveRequest.DateOfLeave)%>
</div>
<div class="editor-field">
<div class="date-container">
<%= Html.TextBoxFor(model => model.LeaveRequest.DateOfLeave.Date)%>
</div>
<div class="time-container">
<%= Html.TextBoxFor(model => model.LeaveRequest.DateOfLeave.TimeOfDay)%>
</div>
<div class="clear">
<%= Html.ValidationMessageFor(model => model.LeaveRequest.DateOfLeave)%>
</div>
</div>
The problem I am having is that the behavior I am getting is not the same that that author explained as should happen. Here is a screenshot of my results.
I must be missing something, but I can't tell what. I have read this article several times and can't see what I am missing. I am guessing there needs to be something to tell the program to us the new EditorTemplates, but I don't know how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您错过了这样一个事实:斯科特的解决方案和您链接到的解决方案都不会执行以下操作:
相反,它们使用:
然后利用自定义编辑器将字段限制为仅日期(而不是日期和时间)。
向下滚动到您链接到的文章的分隔日期/时间字段标题,并仔细阅读有关自定义编辑器模板的信息。
You missed the fact that both Scott's solution and the solution you link to don't do:
Instead they use:
And then utilize a custom editor to limit the field to just the Date (instead of both the date and time).
Scroll down to the Separate the Date / Time fields header of the article you link to and read a little bit closer about custom editor templates.
如果您愿意,还可以在客户端添加默认值。
这会将 Texbox 的初始值设置为当前的 DateTime,但您显然可以将其设置为您喜欢的任何值。
You can also add a default value on the client side if you'd like.
This sets the initial Texbox value to the current
DateTime
but you can obviously set it to whatever you like.