如何让DateCreated字段自动填充DateTime.Now
<div class="editor-label">
@Html.LabelFor(model => model.DateCreated)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DateCreated)
@Html.ValidationMessageFor(model => model.DateCreated)
</div>
答案 0 :(得分:2)
这通常是您在视图范围之外进行的操作,即在控制器中。
public ActionResult MyAction()
{
var model = new MyModel();
model.DateCreated = model.DateCreated ?? DateTime.Now;
return View(model);
}
或者,在模型本身的构造函数中。