开发者

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

<%: 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.


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


  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)%>
    


I would put this into a <td> or <div> and then use the id property to access and blank it via jquery.

<script type="text/javascript">
$(document).ready(function(){
$("#clearme").html("");
});
</script>

<div id="clearme">
<%: Html.EditorFor(model => model.date)%>
</div>

There may be a way using a built in MVC feature but I'm not aware of one.

Here is a quick fiddle that shows you the concept, so you can play around with it if you like.


Some corrections to Jorge's answer:

public DateTime DOB { get; set; } 
[Required]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime? NullableDOB
{
  get => DOB.ToNullable();
  set => DOB = value.DefaultIfNull(); 
}

ToNullible(), DefaultIfNull() and IsNull() are a handy extension functions:

public static T? ToNullable<T>(this T source) where T : struct
  => source.IsNull(default(T)) ? (T?)null : source;

public static T? ToNullable<T>(this T source, T @default) where T : struct
  => source.IsNull(@default) ? (T?)null : source;

public static T DefaultIfNull<T>(this T? source, T @default) where T : struct
  => source ?? @default;

public static T DefaultIfNull<T>(this T? source) where T : struct
  => DefaultIfNull(source, default(T));


public static bool IsNull<T>(this T source) where T : struct
  => source.Equals(default(T));


You can use a small jquery function.

I solve my problem like this:

        @if(Model.HolidayDate <= DateTime.MinValue)
        {
            @Html.TextBoxFor(model => model.HolidayDate, new { @class = "datepick", @style = "width: 75px;", @id="dataInput"})
        }
        else
        {
            @Html.TextBoxFor(model => model.HolidayDate, new { @class = "datepick", @style = "width: 75px;"})
        }

and in the end of the page:

$(function () { $('#dataInput').val(''); });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜