EditorFor(日期):如何显示空文本框?

时间:2023-01-28 12:14:59
<%: 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?

如何在加载时使此代码显示为空文本框? Model.date不可为空,因此始终显示非空值。如何强制它以空值开始?

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.

注意:我不想让它可以为空,因为在实际代码中,它绑定到必须具有值的模型属性(BTW,我有该属性的必需数据注释)。让它可以为空是没有意义的。以今天的默认日期替换它不是一种选择,因为这种情况下的要点是确保用户不要忘记主动指定日期。

TIA.

5 个解决方案

#1


8  

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.

问题是如果Model.Date属性不可为空,那么它将始终具有值。在过去,我为DateTime创建了一个编辑器模板(它也使用了jquery日期选择器)。这允许您处理您的空值,您可能正在寻找等于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.

你不必担心名字的空字符串,因为这实际上只填充了html前缀,这应该足够了。

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

我个人认为我的视图模型中的DateTime可以为空,但在我的域模型中没有使用其间的验证

#2


2  

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

    在您的模型中创建一个兄弟属性并使用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)%>
    

#3


0  

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

我会把它放到或

中然后使用id属性来访问并通过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.

可能有一种方法使用内置的MVC功能,但我不知道一个。

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

这是一个向您展示概念的快速小提琴,如果您愿意,可以随意使用它。

#4


0  

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:

ToNullible(),DefaultIfNull()和IsNull()是一个方便的扩展函数:

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

#5


-1  

You can use a small jquery function.

您可以使用小型jquery函数。

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(''); });

#1


8  

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.

问题是如果Model.Date属性不可为空,那么它将始终具有值。在过去,我为DateTime创建了一个编辑器模板(它也使用了jquery日期选择器)。这允许您处理您的空值,您可能正在寻找等于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.

你不必担心名字的空字符串,因为这实际上只填充了html前缀,这应该足够了。

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

我个人认为我的视图模型中的DateTime可以为空,但在我的域模型中没有使用其间的验证

#2


2  

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

    在您的模型中创建一个兄弟属性并使用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)%>
    

#3


0  

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

我会把它放到或

中然后使用id属性来访问并通过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.

可能有一种方法使用内置的MVC功能,但我不知道一个。

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

这是一个向您展示概念的快速小提琴,如果您愿意,可以随意使用它。

#4


0  

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:

ToNullible(),DefaultIfNull()和IsNull()是一个方便的扩展函数:

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

#5


-1  

You can use a small jquery function.

您可以使用小型jquery函数。

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(''); });