Split DateTime for date and time in ASP.NET MVC

I read a Scott Hanselman post on this topic. I found another article that seems to have a simpler approach. The second of which I decided to try.

However, the first part when creating EditorTemplates does not work for me. I copied DateTime.ascx and TimeSpan.ascx if the author wrote them. Then I divided the fields in my opinion.

    <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 facing is that the behavior I get is not what this author explained how it should happen. Here is a screenshot of my results.

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

- , , . , . , - EditorTemplates, , .

+3
2

, , , , :

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

:

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

, ( ).

/ , , .

+8

, .

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

Texbox DateTime, , , .

+1

Source: https://habr.com/ru/post/1774984/


All Articles