剃刀视图文本框问题

时间:2012-12-08 23:00:38

标签: c# asp.net-mvc-3 razor

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

1 个答案:

答案 0 :(得分:2)

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

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

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