问题
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