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

  • SEARCH
  • Home
  • 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 8742735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:33:03+00:00 2026-06-13T11:33:03+00:00

Is this possible to have one DateTime property in model, but use it as

  • 0

Is this possible to have one DateTime property in model, but use it as two inputs on view/form? For example Date part will be using jqueryui datepicker, while Time part picker will be a masked input, or other nifty jquery plugin. From time to time i need to use two dropdows for time picking (hours and minutes).

My goal is to have one DateTime property in model (no partial strings for date and time). Is this possible? Using MVC4.

  • 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-06-13T11:33:04+00:00Added an answer on June 13, 2026 at 11:33 am

    Here’s a technique i use to split a date into 3 fields, while only having 1 DateTime property on the ViewModel. While not exactly what you’re after, you should be able to use a similar method to achieve what you want.

    Editor Template
    /views/shares/editortempaltes/datetime.cshtml

    @model Nullable<System.DateTime>         
    @Html.TextBox("Day", Model.HasValue ? Model.Value.Day.ToString() : "", new { Type = "Number", @class="date-day" })
    @Html.ValidationMessage("Day")
    @Html.DropDownList("Month", months, new { @class="date-month" })
    @Html.ValidationMessage("Month")
    @Html.TextBox("Year", Model.HasValue ? Model.Value.Year.ToString() : "", new { Type = "Number", @class="date-year" })
    @Html.ValidationMessage("Year")
    

    Custom ModelBinder

    public object GetValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
    {
        int day, month, year;
    
        if (TryGetValue(controllerContext, bindingContext, propertyDescriptor.Name + ".Day", out day) && TryGetValue(controllerContext, bindingContext, propertyDescriptor.Name + ".Month", out month) && TryGetValue(controllerContext, bindingContext, propertyDescriptor.Name + ".Year", out year))
        {
            try
            {
                return new DateTime(year, month, day);
            }
            catch (ArgumentOutOfRangeException)
            {
                var fullPropertyName = bindingContext.ModelName + "." + propertyDescriptor.Name;               
                bindingContext.ModelState[fullPropertyName] = new ModelState();                         
                bindingContext.ModelState[fullPropertyName].Errors.Add("Invalid date");
            }
        }
        return null;
    }
    
    
    private bool TryGetValue(ControllerContext controllerContext, ModelBindingContext bindingContext, string propertyName, out int value)
    {
        var fullPropertyName = bindingContext.ModelName + "." + propertyName;
        string stringValue = controllerContext.HttpContext.Request[fullPropertyName];
    bindingContext.ModelState.Add(fullPropertyName, new ModelState() { Value = new ValueProviderResult(stringValue, stringValue, null) });
        return int.TryParse(stringValue, out value);
    }
    

    Usage
    Add a DateTime property to your ViewModel, then call

    @Html.EditorFor(m => m.DateTimeProperty)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form, with two DateTimePicker-controls: One for date, and one for
I have a sortable list like this one: http://jqueryui.com/demos/sortable Is it possible to get
Is this possible to have two (or more) different kinds of cells to be
Is this possible? We have WinForms (CF) apps which use the EMDK, and would
In my script I need to make a cycle like this one: use DateTime;
Is something like this possible: I have a flash animation of a glowing orb.
Is this possible? I have a solution with both projects, I just want to
is this possible to do , or do i have to do it all
Is this possible at all? The idea is to have the following: if ($some_statement)
Is this possible? In an events system an event can have multiple times. (ie,

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.