Samantha J T Star
Samantha J T Star

Reputation: 32798

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.

Upvotes: 4

Views: 2312

Answers (3)

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13574

for a nullable datatime you need to use DateTime? type in your model. This way EndDate will have null until it's assigned a value :-)

Upvotes: 1

nemesv
nemesv

Reputation: 139758

Make EndDate nullable:

public DateTime? EndDate { get; set; } 

Upvotes: 4

Jan
Jan

Reputation: 16042

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);

Upvotes: 3

Related Questions