Search code examples
c#.netasp.net-mvcrazor

MVC 5.2 How to set default value for datetime-local?


 @Html.EditorFor(m => m.DateOfAcception, new
                {
                    htmlAttributes = new { type = "datetime-local", required = "required" },
                    @class = "form-control datepicker",
                    @value = DateTime.Now.ToString()
                })

I got something like that but the Value is not working also my model is

public DateTime DateOfAcception { get; set; } = DateTime.Now;

But i cant get it to start with default value.

This is how it looks in html

enter image description here

The part Novo is just @Html.EditorFor(m => m.DateOfAcception) if the left side part can start with that value like that somehow


Solution

  • You should use TextBoxFor and put international time and then Razor will convert it to local date time. I hope this will answer your question.

      @Html.TextBoxFor(x => x.DateOfAcception, "{0:yyyy-MM-ddTHH:mm:ss}", new
                           {
                               @class = "form-control",
                               required = "required",
                               @type = "datetime-local"
                           })