EditorFor(date): How to display empty text box?

18,730

Solution 1

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

Solution 2

  1. In your model create a sibling property and decorate it with required:

    [Required]
    DateTime? CustomDate {
       get { return date==default(DateTime)?null:date; }
       set { date = value; }
    }
    
  2. In your view replace your input with :

    <%:Html.EditorFor(model => model.CustomDate)%>
    
Share:
18,730
Serge Wautier
Author by

Serge Wautier

Self appointed Internationalization Specialist, author of appTranslator, which was once a great tool to translate the UI of your programs. No longer since I never took the necessary time to keep it up with recent frameworks... Currently managing a great team that develops and operates Talk2M: One of the largest OpenVPN setup worldwide (Yes sir!)

Updated on June 13, 2022

Comments

  • Serge Wautier
    Serge Wautier almost 2 years
    <%: 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.

  • Mark
    Mark over 13 years
    Personally I would have the DateTime in my view model nullable but not on my domain model using the validation in between.
  • Serge Wautier
    Serge Wautier over 13 years
    Mark, this sounds very interesting. Will try it immediately. BTW, in your comment, you read in my mind. That's what I was going to do in absence of a nice solution. Nitpickers may even argue that only null may be matched to an empty string. Not DateTime.MinValue. I must say that I disagree since an empty string should never touch back the server: It must be ruled out by client-side validation.
  • Serge Wautier
    Serge Wautier over 13 years
    Jon, thanks for your reply. Too far away from the "separation of concerns" principle though.
  • JonVD
    JonVD over 13 years
    I'm not so sure it does, but I don't know your application either! I'm seeing this as a UI issue, you want a certain UI field to be blank instead of populated on load. This could either be accomplished at the server side, be that data access or business logic (I like the view model ideas), or the client presentation layer (using the above jquery). In terms of SoC your not really completing any business or data access logic in your presentation layer, merely changing the way you want a field displayed, falling well within the presentation layer imo. But that is my interpretation, not yours =)
  • Serge Wautier
    Serge Wautier over 13 years
    my interprétation is that you add a div id and some client side code to fix a server side issue.