Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop date showing as default in MVC3 when not set?

In my view I have the following:

@Html.TextBoxFor(model => model.EndDate)

When the code and the model is created I see it sets a default date rather than null for the field that's defined as follows:

public DateTime EndDate { get; set; }

In my view I see the following:

{1/1/0001 12:00:00 AM}

Is there some way that I can make it show/return an empty string if the field is not yet set to a value by my code. Here it just defaults to the above when I create a view and don't set that field.

like image 212
Samantha J T Star Avatar asked Feb 23 '23 10:02

Samantha J T Star


2 Answers

Make EndDate nullable:

public DateTime? EndDate { get; set; } 
like image 136
nemesv Avatar answered Mar 11 '23 10:03

nemesv


DateTime is a value type which cannot have no value.

You have to use nullable of DateTime written like

public DateTime? EndDate { get; set; }

Now you can assign the null value to your model in the controller:

return View(null);
like image 28
Jan Avatar answered Mar 11 '23 09:03

Jan