I have a Textbox
with type as date
. I am trying to set default value of the textbox
to current date.
@Html.TextBoxFor(x => x.Date, new { @id = "Date", @type = "date",
@value = DateTime.Now.ToShortDateString() })
The above line doesn't set default value. How to set default value as current date?
Another Solution:
It works on my end, sure it will work on yours.
It's better way to manage in view
As Stephen Muecke said, you need to set the property's value on the model.
And your Razor would be:
Note that the
id
and thename
properties should be automatically assigned to the property name when using aFor
method, such as@Html.TextBoxFor()
, so you don't need to explicitly set theid
attribute.