0

这是我在 ASP.NET MVC3 中的 Create.cshtml 的代码片段。我不知道如何将 DateAdded 保存为 DateTimeNow。我使用 CRUDE 创建新记录。我已经尝试删除下面的代码并将其替换为 DateTimeNow(系统时间)而不显示下面代码中的文本框和标签:

<div class="editor-label">
    @Html.LabelFor(model => model.DateAdded)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.DateAdded)
    @Html.ValidationMessageFor(model => model.DateAdded)
</div>

这是我的 Create.cshtml 片段

@model PhoneBook.Models.Contact

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
<fieldset>
    <legend>Contact</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.DateAdded)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.DateAdded)
        @Html.ValidationMessageFor(model => model.DateAdded)
    </div>

  <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
4

1 回答 1

3

您应该DateTime.Now在控制器中给出值:

[HttpPost]
public ActionResult Foo(Contact model)
{
    model.DateAdded = DateTime.Now;
    ...
    ...
}

或者,如果您希望日期时间在呈现时出现在页面中:

[HttpGet]
public ActionResult Foo()
{
    Contact model = new Contact{ DateAdded = DateTime.Now};

    return View(model);
}
于 2012-05-18T09:23:03.557 回答