Darkmage
Darkmage

Reputation: 1603

Razor view text-box issue

How can i get the DateCreated field to auto populate with 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>

Upvotes: 0

Views: 97

Answers (1)

McGarnagle
McGarnagle

Reputation: 102743

This is normally something you'd do outside of this scope of the view, ie, in the controller.

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

Or, in the constructor of the model itself.

Upvotes: 2

Related Questions