简体   繁体   中英

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

在此处输入图像描述

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

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"
                       })

Thank you very much: this helped me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM