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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:02:58+00:00 2026-05-18T11:02:58+00:00

<%: Html.EditorFor(model => model.date)%> How can I make this code display an empty textbox

  • 0
<%: Html.EditorFor(model => model.date)%>

How can I make this code display an empty textbox when loaded? Model.date is not nullable, hence always displays a non-empty value. How can I force it to start with an empty value?

Note: I don’t want to make it nullable because in the real code, it’s tied to a model property which must have a value (BTW, I have a Required data annotation for that property). It doesn’t make sense to make it nullable. Replacing it by a default date such as today’s is not an option neither because the point in this case is to make sure user doesn’t forget to actively specify a date.

TIA.

  • 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-18T11:02:59+00:00Added an answer on May 18, 2026 at 11:02 am

    The problem is if the Model.Date property is not nullable then it will always have a value. In the past I have created an editor template for DateTime (which also utilises the jquery date picker). This allows you to handle your empty value for which you’d presumably be looking out for the date equaling DateTime.MinValue.

    So your editor template would look something like:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime>" %>
    <%=Html.TextBox("", (Model != DateTime.MinValue ? Model : string.Empty)) %>
    

    You needn’t worry about the empty string for name as this actually gets populated with just the html prefix which should suffice.

    This article also describes how to create the editor template.

    http://geekswithblogs.net/michelotti/archive/2010/02/05/mvc-2-editor-template-with-datetime.aspx

    Personally I would have the DateTime in my view model nullable but not on my domain model using the validation in between

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

Sidebar

Related Questions

Why can't I pass in html attributes to EditorFor() ? eg; <%= Html.EditorFor(model =>
Here's the item from the View @Html.EditorFor(Function(model) model.BirthDate) Here's the code from the EditorTemplate
I'm using MVC3. When I make a field with @Html.EditorFor(model => model.RowKey) Then the
I am getting some unexpected behavior from Html.EditorFor(). I have this controller: [HandleError] public
Lets say I have... @Html.EditorFor(i => i.Date) ... and I have a template defined
@using (Html.BeginForm(Edit, MyController, FormMethod.Post, new { enctype=multipart/form-data})) { @Html.EditorFor(model => model.Name) <input type=file name=fileUpload
Html.TextBox(ParentPassword, , new { @class = required }) what the gosh darned heck is
html Tidy gives this as output for some reason: <?xml version=1.0 encoding=utf-16?> <?xml version=1.0
The html helper methods check the ViewDataDictionary for a value. The value can either
While HTML Scraping is pretty well-documented from what I can see, and I understand

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.