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 8817469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:51:24+00:00 2026-06-14T04:51:24+00:00

I have an Editor Template for my Date fields which sets the current date

  • 0

I have an Editor Template for my Date fields which sets the current date if the field is null.

@model Nullable<DateTime>

@{
    var date = Model as DateTime?; // local reference to a DateTime? 

    if (!date.HasValue                     // check if it is not null 
        || date.Equals(default(DateTime))) // check if it is not default 01/01/0001
    {
        date = DateTime.Now;
    }

    @Html.TextBox("", String.Format("{0:d}", date.Value.ToShortDateString()), new { @class = "date" })
 }

I would like to apply this template to only my Create Views.

When I edit a record the above code populates the current date for fields that I had intentionally left as null as the date was unknown.

Wondering if my code can be amended to accommodate this?

Any help would be appreciated.

  • 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-14T04:51:25+00:00Added an answer on June 14, 2026 at 4:51 am

    There are a couple ways you could do this.

    First way is to pass in a flag through your ViewData, something like this:

    @Html.EditorFor(m => m.SomeDate, new { nowIfNull = true })
    

    Then update your template:

    @model Nullable<DateTime>
    
    @{
        var date = Model as DateTime?; // local reference to a DateTime? 
    
        if ((!date.HasValue                     // check if it is not null 
            || date.Equals(default(DateTime))) // check if it is not default 01/01/0001
            && ((bool?)ViewData["nowIfNull"] ?? false)) // check if it should set to now if null
        {
            date = DateTime.Now;
        }
    
        @Html.TextBox("", date == null ? String.Empty : String.Format("{0:d}", date.Value.ToShortDateString()), new { @class = "date" })
     }
    

    Another way is to default this to DateTime.Now in your controller — or even better, a mapper — when you create a new model. This separates the concerns of setting the default where it belongs, in your controller/mapper instead of the view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hava an editor template for, let's say, date: @model DateTime @section AdditionalJavaScript2 {
I have the following editor template @model DateTime? @Html.TextBox(, (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty),
is it possible to override an editor template? I have DateTime fields in my
I have some DateTime fields on my MVC model classes - some which require
I have created this editor template: @model DateTime? @using MyMvcApp.Properties <div id=dateTimePicker_@(ViewData.ModelMetadata.PropertyName)> <script type=text/javascript
I have created an editor template for one of my models in my current
I have an editor template (Views/Shared/EditorTemplates/HORDER.ascx) which inherits System.Web.Mvc.ViewUserControl<CCOK2.Models.HORDER> My viewmodel has a one-to-many
I've got an editor template which renders out a checkbox: @Html.CheckBoxFor(model => model.Follow) Which
Let's say in my model I have public DateTime Date {get;set;} property. On the
I'm writing an editor template for entering a date and time which is made

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.