0

如何让 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>
4

1 回答 1

2

这通常是您在视图范围之外(即在控制器中)执行的操作。

public ActionResult MyAction()
{
    var model = new MyModel();
    model.DateCreated = model.DateCreated ?? DateTime.Now;
    return View(model);
}

或者,在模型本身的构造函数中。

于 2012-12-08T23:16:26.253 回答