Set default input value Datetime.Now on create view MVC ASP.NET Core 2?

南笙酒味 提交于 2021-01-29 10:37:02

问题


How can I set default value of input OpenDate is date and time of server.

<div class="form-group">
   <label asp-for="OpenDate" class="control-label">Ngày đăng ký</label>
   <input asp-for="OpenDate" class="form-control" />
   <span asp-validation-for="OpenDate" class="text-danger"></span>
</div>

回答1:


In your model:
[BindNever]
public System.DateTime OpenDate { get; set; }

In your Controller (where you are posting the form)
yourModel.OpenDate = DateTime.Now;

In your View:
You can exclude the code that you have posted, because it will not be needed.

Note: I used "BindNever" because you can exclude this property from the model binding. It is a data that the system will get for you.



来源:https://stackoverflow.com/questions/50249234/set-default-input-value-datetime-now-on-create-view-mvc-asp-net-core-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!