-1

我有这样的形式

@using (Html.BeginForm()) {

    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Course</legend>

        @Html.HiddenFor(model => model.CId)

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

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

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

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

我想在日期字段的字段中插入系统当前日期和时间。我怎样才能做到这一点。模型和控制器已经通过脚手架创建。

4

3 回答 3

2

在调用编辑器之前插入它(假设您的属性是 a DateTime

@{ Model.date = DateTime.Now; }

如果您的属性是字符串,请调用DateTime.Now.ToString().

于 2012-08-24T06:52:47.143 回答
1

您可以在相应的控制器操作中设置默认值:

public ActionResult Index() {
    return View(new SomeModel { date = DateTime.Now });
}
于 2012-08-24T06:54:17.727 回答
1

如下所述更改您的代码

@{ Model.date = DateTime.Now.ToString(); }
@Html.ValidationSummary(true)
<fieldset>
    <legend>Course</legend>

    @Html.HiddenFor(model => model.CId)

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

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

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

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
于 2012-08-24T06:54:51.710 回答