在 ASP.NET MVC 中将 DateTime 拆分为日期和时间

发布于 2024-10-02 08:30:51 字数 1331 浏览 0 评论 0原文

我已阅读 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.

Screenshot showing full DateTime in first field and all zeros in the second

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离去的眼神 2024-10-09 08:30:51

您错过了这样一个事实:斯科特的解决方案和您链接到的解决方案都不会执行以下操作:

<%= Html.TextBoxFor(model => model.LeaveRequest.DateOfLeave.Date) %>

相反,它们使用:

<%= Html.EditorFor(model => model.LeaveRequest.DateOfLeave.Date) %>

然后利用自定义编辑器将字段限制为仅日期(而不是日期和时间)。

向下滚动到您链接到的文章的分隔日期/时间字段标题,并仔细阅读有关自定义编辑器模板的信息。

You missed the fact that both Scott's solution and the solution you link to don't do:

<%= Html.TextBoxFor(model => model.LeaveRequest.DateOfLeave.Date) %>

Instead they use:

<%= Html.EditorFor(model => model.LeaveRequest.DateOfLeave.Date) %>

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.

油饼 2024-10-09 08:30:51

如果您愿意,还可以在客户端添加默认值。

<%= Html.TextBoxFor(model => model.LeaveRequest.DateOfLeave.Date,
new { @Value= DateTime.Now })%> 

这会将 Texbox 的初始值设置为当前的 DateTime,但您显然可以将其设置为您喜欢的任何值。

You can also add a default value on the client side if you'd like.

<%= Html.TextBoxFor(model => model.LeaveRequest.DateOfLeave.Date,
new { @Value= DateTime.Now })%> 

This sets the initial Texbox value to the current DateTime but you can obviously set it to whatever you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文