How can I stop the default date displayed in MVC3 when it is not set?

In my opinion, I have the following:

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

When the code and model are created, I see that it sets the default date instead of zero for the field, which is defined as follows:

 public DateTime EndDate { get; set; } 

In my opinion, I see the following:

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

Is there a way so that I can show / return an empty string if the field is not already set by the value in my code. Here it just defaults to the value above when I create the view and do not set this field.

+4
source share
3 answers

DateTime is a value type that cannot have a value.

You must use the NULL value for DateTime, written as

 public DateTime? EndDate { get; set; } 

Now you can assign null your model in the controller:

 return View(null); 
+3
source

Make EndDate nullable :

 public DateTime? EndDate { get; set; } 
+4
source

for zero time, do you need to use a DateTime? type DateTime? in your model. That way, EndDate will be null until a value is assigned :-)

+1
source

Source: https://habr.com/ru/post/1380723/


All Articles