2

I have 2 pages. A create page and an edit page. When I choose a date on the create page and submit it, it all goes right. But when I read the date out the database and set it in the input field, it won't display the date.

When I submit the create page, the date looks like 9/05/2016 0:00:00 in the database.

Create page:

@Html.EditorFor(model => model.Date, new{htmlAttributes = new{@class = "form-control",type = "date"}})

edit page:

@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control", @Value = ViewBag.Date, type = "date" } })

Domain:

public DateTime Date{ get; set; }
  • 3
    Never set the `value` attribute. You set the value of the `Date` property before you pass the model to the view. And your generating `type="date"` to generate the Chrome or Edge HTML-5 datepicker, which means you need `@Html.TextBoxFor(m => m.Date, "{0:yyyy-MM-dd}", new { type = "date", @class = "form-control" })` –  May 26 '16 at 11:45
  • See this [answer](http://stackoverflow.com/questions/6978631/how-to-set-date-format-in-html-date-input-tag) – Mukesh Adhvaryu May 26 '16 at 12:15
  • I need to set the value before I pass the model. Because when the user opens the edit page he can see what the date is at the moment. But he can change the date with a datepicker. –  May 26 '16 at 17:38
  • I have no idea what you mean by your last comment. You need to use the code in my first comment. And I repeat - do not use `new { @Value = ViewBag.Date }` - set the value of `model.Date` in the GET method before you pass teh model to the view and get rid of that `ViewBag` property –  May 27 '16 at 01:45

0 Answers0