Displaying today`s date using DisplayFormat
I am using
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yy}")]
but the problem im having is it
s always displaying 01-Jan-01 if the date value is null.
I am also using jquery for the display as follows:
$("#DateFrom").datepicker({
开发者_如何转开发 dateFormat: 'dd-M-yy',
altField: 'dd-M-yy',
altFormat: 'd/m/y',
minDate: new Date(2010, 1 - 1, 1),
maxDate: '+12m +1w'
});
I want to dipsly today`s date by default if the date value is null.
You could set it in your controller action:
public ActionResult Index()
{
var viewModel = new MyViewModel();
viewModel.DateFrom = FetchDateFromSomewhere() ?? DateTime.Now;
return View(viewModel);
}
or in the getter of your view model:
private DateTime? _dateFrom;
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yy}")]
public DateTime? DateFrom
{
get
{
return _dateFrom ?? DateTime.Now;
}
set
{
_dateFrom = value;
}
}
精彩评论