Share via


Some Trick to Force Empty "" Value in Razor Date?

Question

Friday, January 17, 2020 7:11 PM

I need to set the date textbox empty "".

I have an imput date on my Index:

 @Html.InputDateFormFor(m => m.Category.StartDate, new { @id = "SinceDate"}) 

StartDate is a property of the Category Entity. 

So i could use something like StartDate=null; however this variable is set as not nulleable. 

public DateTime StartDate { get; set; }

When the page is loaded, the StartDate imput field has a default value similar to 01/01/0001 wich causes some problems and i cant modify the internal code (Entitys).

There is a Trick that i could use on the Index Side to set Empty value to my variable? that will trigger the validation on the field to force the user to put something on the imput Date.

All replies (2)

Friday, January 17, 2020 9:37 PM ✅Answered

Use a standard nullable type.

public DateTime? StartDate { get; set; }

/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types


Monday, January 20, 2020 7:46 AM ✅Answered

Hi MVC_user,

You can use this line to set the value to empty.

document.getElementById("StartDate").value = "";

Best Regards,

Jiadong Meng