5

my Razor form code

<div class="form-group">
    @Html.LabelFor(model => model.achPayDate, htmlAttributes: new { @class = "control-label col-md-7" })
    @Html.DisplayFor(model => model.achPayDate, new {@class="date"})
</div>

my cs code

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? achPayDate { get; set; }

01/01/0001 is displayed when the achPayDate=0001-01-01T00:00:00 , I want to suppress the date to be blank. How can I do that??

user6815384
  • 219
  • 1
  • 2
  • 15

4 Answers4

5

You can do the following

  1. In your MVC Project => Views => Shared , create new folder DisplayTemplates
  2. Create new view under DisplayTemplates folder and call it Date.cshtml

the code of Date.cshtml

@model DateTime?
@if(Model!=null && Model!=DateTime.MinValue)
{
   @string.Format("{0:MM/dd/yyyy}",Model)
}

the code of your Model

[DataType(DataType.Date)]
public DateTime? achPayDate { get; set; }

this way will make all DateTime with data annotation [DataType(DataType.Date)] to follow same behavior, and also you can do the same for the EditorTemplates

you can get more information about EditorTemplates and DisplayTemplates here

Hope this will help you

Monah
  • 6,714
  • 6
  • 22
  • 52
  • `[DataType(DataType.Date)]` has nothing to do with the template (its for generating `type="date"` when using `@Html.EditorFor(m => m.achPayDate )` –  Sep 19 '16 at 22:37
  • in my answer I focused on the `DisplayTemplate` and I mentioned that he can apply the `EditorTemplate` also, `DataType.Date` will automatically use the `DisplayTemplate` "date.cshtml" if it was exists in the `DisplayTemplates` folder , as long as the user didn't assign the `UIHint("...")` – Monah Sep 19 '16 at 22:39
  • `DataType.Date` will **not** _automatically use a template_. It has nothing to do with templates. Templates are selected because either the name of the template matches the model (e.g. `DateTime.cshtml`) or because of the `[UIHint]` attribute. The sole purpose of `[DataType(DataType.Date)]` is to render `type="date"` when using `EditorFor()` with the standard inbuilt template –  Sep 19 '16 at 22:43
  • if you defined 2 display templates ( date.cshtml and datetime.cshtml) and you used `DataType.Date` , what will happen on the view for the code `@Html.DisplayFor(m=>m.achPayDate)` ? – Monah Sep 19 '16 at 22:49
  • `DataType.Date` has nothing to do with it. Must admit I have not tested what would happen if you have 2 display templates in the same folder with the same `@model` - will test it later :) –  Sep 19 '16 at 22:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123740/discussion-between-stephen-muecke-and-hadi-hassan). –  Sep 19 '16 at 23:01
3

For normal DateTimes, if you don't initialize them at all then they will match DateTime.MinValue, because it is a value type rather than a reference type.

You can also use a nullable DateTime, like this:

DateTime? MyNullableDate;

Or the longer form:

Nullable<DateTime> MyNullableDate;

or use that:

private DateTime? _achPayDate = null;
public DateTime? achPayDate { get {return _achPayDate ; } set{_achPayDate = value;} }
Husni Salax
  • 1,968
  • 1
  • 19
  • 29
  • OP's property is already nullable :) (and you cannot bind fields - they need to be properties) –  Sep 19 '16 at 21:48
  • I have the Web service return the DateTime.MinValue and I just wanted to suppress the 01/01/0001 date to be blank. Can I add code on the Razor? Thanks! Your method return the Date too 1/1/0001--FYI. – user6815384 Sep 19 '16 at 21:56
  • @user6815384, Just check the value in the controller and set it to `null` if its equal to `DateTime.MinValue` –  Sep 19 '16 at 22:48
1

I had 1900-01-01 as "Null" date and suppressed it this way: Date.cshtml (in DisplayTemplates):

@model DateTime?
@if (Model != null && Model == DateTime.Parse("1900-01-01 00:00:00"))
{
    Html.Raw("&nbsp;");
}
@if (Model != null && Model != DateTime.Parse("1900-01-01 00:00:00"))
{
    @string.Format("{0:yyyy-MM-dd hh:mm:ss}",Model)
}

I used it in a view by its template name "Date":

@Html.DisplayFor(modelItem => item.m.CarrierArrival,"Date")
ZygD
  • 22,092
  • 39
  • 79
  • 102
-1

I added like below in web grid column

grid.Column("Note Date",
style: "width:159px",
format: @<text>
@if (item.NoteDate == DateTime.MinValue)
{
                    
}
else
{
  @string.Format("{0:MM/dd/yyyy}", item.NoteDate)
}
</text>)
Mr doubt
  • 51
  • 1
  • 10
  • 42