Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding the default value for a date

My View Model:

public partial class FileTransferFilterCriteriaViewModel
{
    public string Fice { get; set; }
    public string SourceEmail { get; set; }
    public string TargetEmail { get; set; }
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }
    public string Status { get; set; }
    public string Category { get; set; }
}

(Nothing is coming from the DB.)

My Controller:

return View(new FileTransferFilterCriteriaViewModel())

Here is what gets displayed for both FromDate and ToDate:

1/1/0001 12:00:00 AM

My HTML:

@Html.TextBoxFor(x =>x.Criteria.FromDate)

Questions:

  1. If the date is null, how can I suppress the display of the default date value?
  2. If the date is not null, how can I format the date as MM/dd/yyyy?
like image 672
learning... Avatar asked Dec 07 '22 12:12

learning...


1 Answers

Use nullable date in your ViewModel:

public DateTime? FromDate { get; set; }
like image 65
lahsrah Avatar answered Jan 28 '23 10:01

lahsrah