How to assign a default date value to a Textboxfor?

Asked

Viewed 279 times

2

I own the following Textboxfor

@Html.TextBoxFor(model => model.data, "{0:dd/MM/yyyy}", new { @class = "form-control", @alt = "date", @placeholder = DateTime.Now.Date.ToString("dd/MM/yyyy"), @maxlength = "10", @data_val="false"})

As you can see, there is a placeholder that defines what will appear on it when the screen opens. My question is, How do I put today’s date in the actual content of Textboxfor? For, when I open the screen without filling the textbox it accuses that this empty, ie being displayed that today’s date is set in the field, but only visually.

Thank you.

  • 1

    Ever tried to add @value = DateTime.Now.Date.ToString("dd/MM/yyyy")?

  • Good afternoon. I tried to perform your tip, but it still didn’t work. When I open the screen and do the 'Submit' with the form button below the date field is displayed the error message (previously made by me) as if the field is empty. Would you have any suggestions to be made at Controler?

  • I realized that the problem was not to use value = "". This is correct, but for a small detail, one should use Value = " (capital 'v'). Thanks for the tip. Worked well.

1 answer

1


Like the Pablo Carvalho commented, Voce should add the property value in its object

@Html.TextBoxFor(model => model.data, "{0:dd/MM/yyyy}", new { @class = "form-control", 
@alt = "date", @placeholder = DateTime.Now.Date.ToString("dd/MM/yyyy"), 
@maxlength = "10", @data_val="false", @value = DateTime.Now.Date.ToString("dd/MM/yyyy")})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.