Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4019490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:06:39+00:00 2026-05-20T10:06:39+00:00

I have created this editor template: @model DateTime? @using MyMvcApp.Properties <div id=dateTimePicker_@(ViewData.ModelMetadata.PropertyName)> <script type=text/javascript

  • 0

I have created this editor template:

@model DateTime?

@using MyMvcApp.Properties

<div id="dateTimePicker_@(ViewData.ModelMetadata.PropertyName)">
    <script type="text/javascript" language="javascript">
        //<![CDATA[
        $(document).ready(function () {
            var $div = $('#dateTimePicker_@(ViewData.ModelMetadata.PropertyName)');

            $div.find('.date').datepicker({ altFormat: 'dd-mm-yy' });
        });

        function clearDateTimePicker_@(ViewData.ModelMetadata.PropertyName)() {
            var $div = $('#dateTimePicker_@(ViewData.ModelMetadata.PropertyName)');

            $div.find('.date').val('');
            $div.find('.hour').val('00');
            $div.find('.minute').val('00');
        }
        //]]>
    </script>

    @* Date - should equal DatePicker.cshtml *@
    @Html.TextBox("Value.Date", Model.HasValue ? Model.Value.Date.ToString() : string.Empty, new { @class = "date" })
    <img alt="@Resources.SelectDate" src="../../../images/calendar.png" class="calendarIcon" />

    @* Time - should equal TimePicker.cshtml *@
    @Html.DropDownList("Value.Hour", new SelectList(new[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }, Model.HasValue ? Model.Value.Hour.ToString("D2") : "00"), 
        null, new { style = "width: auto; margin-left: 5px;", @class = "hour" })
    :
    @{
        List<string> availableMinutes = new List<string>();
        for (int minute = 0; minute < 60; minute += 1)
        {
            availableMinutes.Add(minute.ToString("D2"));
        }

        @Html.DropDownList("Value.Minute", new SelectList(availableMinutes, Model.HasValue ? Model.Value.Minute.ToString("D2") : "00"), 
            null, new { style = "width: auto;", @class = "minute" });
    }
    <img alt="@Resources.SelectTime" src="../../../images/icon_clock_2.gif" style="margin-right: 5px" />
    <input type="button" value="@Resources.Clear" class="ui-state-default" onclick="javascript:clearDateTimePicker_@(ViewData.ModelMetadata.PropertyName)()" />
</div>

As you can see, what I am trying to accomplish is that the user can enter a date/time. However, as input I want to use DateTime? (nullable) because it might be possible the user doesn’t want to select ANY date/time.

This EditorTemplate does not work when the input model is null. Is there any way to create an EditorTemplate that accepts null values and then can still fill the value if entered by the user?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:06:40+00:00Added an answer on May 20, 2026 at 10:06 am

    Ok, I finally nailed down the problem. My solution in words:

    I use a hidden field that stores the current value in textual representation. This hidden field is always translated to the model itself (then it correctly translates to null or a value including hour and minutes). Every time an input element is changed, I update the local value which is being posted back to the server.

    My solution in code:

    @model DateTime?
    
    @using Narcast.Server.Properties
    
    <div id="dateTimePicker_@(ViewData.ModelMetadata.PropertyName)">
        <script type="text/javascript" language="javascript">
            //<![CDATA[
            $(document).ready(function () {
                var $div = $('#dateTimePicker_@(ViewData.ModelMetadata.PropertyName)');
    
                $div.find('.date').datepicker({ altFormat: 'dd-mm-yy' });
            });
    
            function clearDateTimePicker_@(ViewData.ModelMetadata.PropertyName)() {
                var $div = $('#dateTimePicker_@(ViewData.ModelMetadata.PropertyName)');
    
                $div.find('.date').val('');
                $div.find('.hour').val('00');
                $div.find('.minute').val('00');
    
                updateData_@(ViewData.ModelMetadata.PropertyName)();
            }
    
            function updateData_@(ViewData.ModelMetadata.PropertyName)() {
                var $div = $('#dateTimePicker_@(ViewData.ModelMetadata.PropertyName)');
    
                var $date = $div.find('.date').val();
                var $hour = $div.find('.hour').val();
                var $minute = $div.find('.minute').val();
    
                var $data = '';
    
                if ($date != '')
                {
                    $data = $date + ' ' + $hour + ':' + $minute;
                }
    
                $div.find('.data').val($data);
            }
            //]]>
        </script>
    
        @* Hidden field holding the client data *@
        @Html.Hidden("", Model.HasValue ? Model.Value.ToShortTimeString() : string.Empty, new { @class = "data" })
    
        @* Date - should equal DatePicker.cshtml *@
        <input type="text" class="date" onchange="javascript:updateData_@(ViewData.ModelMetadata.PropertyName)()" />
        @*@Html.TextBox("Date", Model.HasValue ? Model.Value.Date.ToString() : string.Empty, new { @class = "date" })*@
        <img alt="@Resources.SelectDate" src="../../../images/calendar.png" class="calendarIcon" />
    
        @* Time - should equal TimePicker.cshtml *@
        <select class="hour" style="width: auto; margin-left: 5px;" onchange="javascript:updateData_@(ViewData.ModelMetadata.PropertyName)()">
        @{
            for (int hour = 0; hour < 24; hour += 1)
            {
                <text><option @{ if (Model.HasValue && Model.Value.Hour == hour) { <text>selected="selected"</text> }}>@(hour.ToString("D02"))</option></text>
            }        
        }
        </select>
    
        :
    
        <select class="minute" style="width: auto; margin-left: 5px;" onchange="javascript:updateData_@(ViewData.ModelMetadata.PropertyName)()">
        @{
            for (int minute = 0; minute < 60; minute += 1)
            {
                <text><option @{ if (Model.HasValue && Model.Value.Minute == minute) { <text>selected="selected"</text> }}>@(minute.ToString("D02"))</option></text>
            }        
        }
        </select>
        <img alt="@Resources.SelectTime" src="../../../images/icon_clock_2.gif" style="margin-right: 5px" />
        <input type="button" value="@Resources.Clear" class="ui-state-default" onclick="javascript:clearDateTimePicker_@(ViewData.ModelMetadata.PropertyName)()" />
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: I have a document I created using LaTeX (my resume in this case),
I have created an editor template for representing selecting from a dynamic dropdown list
I have created an web service I need to publish this service. I need
I have created an Excel 2003 add-in that uses the CLR 2.0 and this
I have created a basic WCF service in IIS. I am aware that this
I have an ISAm table in mySql that was created similar to this: create
Ok so I have this regex that I created and it works fine in
I have a table that is created in a DataList in ASP.Net. This table
I have a report that I created with Crystal Reports 2008. This report uses
I have a model similar to this: public class myModel { public ClassA ObjectA

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.