开发者

Set Nullable DataTime + MVC + ADO Entity Framework

I have a problem that is a pain in the ass. See the next code:

<%: Html.TextBoxFor(model => model.birth_date, new { @id = "datepicker")%>
<%: Html.ValidationMessageFor(model => model.birth_date)%>

When I submitting the form the value for birth_date is 01/01/0001 12:00:00 a.m. how do i stop this? I need a blank textbox.

I have this in my model:

[MetadataType(typeof(Person_Validation))]
public partial class Person{
        }


public class Person_Validation{ 

[Required(ErrorMessage = "Needed Date"开发者_JAVA技巧)]
[DataType(DataType.Date, ErrorMessage = "Please this format: dd/mm/yyyy")]

public DateTime? birth_date{
                    get;
                    set;
                }
    }

Some people said me that I only have to add a mark (?) or Nullable to Datatime, but I did this.

public DateTime? birth_date, and doesnt work for me. Please I need your help!!

Also I have to say that the main model Person was generated by ADO Entity Framework


Three possibilities:

  1. Change the date field in the database to nullable and then regen you EDMX.

  2. I'm assuming your are using Sql Server, you can change the datefield from datetime to datetime2.

  3. You can catch the birthdate in the UI before it's rendered to the user and check for DateTime.MinValue

    if(model.birth_date == DateTime.MinValue) { ... make magic. }

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜