I have a problem when I want to show the DateTime
in an input date. The point is that I bring a date from the database through a parameter query and save it in an object by doing:
usuario.FechaDeNacimiento = Convert.ToDateTime(tablaResultado.Rows[0]["FechaDeNacimiento"]);
Then in the controller I create a manager, I execute the query and the object data is passed to the viewbag
.
In that view I have a input="date"
and I try to display it with value="@ViewBag...."
it but it doesn't work.
Any help or suggestion?
I imagine that in your view you have something like this:
The problem is that to initialize the value of a
input
date type (in browsers that support it), you have to pass the value in the specific year-month-day format or it won't be recognized as a valid date. So in your controller you need to convert the date to a string :You must first create a model using data annotations
Then in the add view also your view has to be strongly typed