You must Sign In to post a response.
  • Category: ASP.Net MVC

    Display blank value if date column contains NULL

    In my mvc application, I have modal as,
    public class Leave
    public DateTime CreatedDate{ get; set; }

    In view, code is like,
    @Html.DisplayFor(modelItem => item.CreatedDate)

    Here, I have an issue, if this date is NULL, it display value, 01-01-0001.
    Here, I want to keep column value as blank. How can i do that without changing datatype of column.
  • #768240

    As you mentioned like you don't want to change the datatype but what to showcase it has NULL.
    You can go for Nullable Date parameter.

    Nullable < DateTime > nullDateTime; // This for Date along with Timestamp

    You can declare this nullable datatype as datetime and then you can use it as regular datatype.


    Nullable nullDT;

    Once you assign the values to nullDT you can check for the null ability.

    if (nullDT != null)
    Alert("Its not Null");


  • #768243
    Here is the example code snippet for how to display blank value if date column contains NULL
     IF ISNULL([Date]) THEN ""
    ELSE STR(YEAR([Date])) + "-" + STR(MONTH([Date])) + "-" + STR(DAY([Date]))

    Useful reference :

  • #768251
    Your datetime field needs to be nullified, otherwise when the class instance is created, compiler sets it to minimum datatime value i.e. '01-01-0001'.
    Use this:

    [Display(Name = "Created Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<DateTime> CreatedDate{ get; set; }

  • Sign In to post your comments