我的形式像
@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>
}
我想在Date字段的字段中插入系统当前日期和时间。我怎样才能做到这一点。模型和cotroller已经通过脚手架创建。
答案 0 :(得分:2)
在调用编辑器之前插入此内容(假设您的属性为DateTime
)
@{ Model.date = DateTime.Now; }
如果您的媒体资源是字符串,请拨打DateTime.Now.ToString()
。
答案 1 :(得分:1)
您可以在相应的控制器操作中设置默认值:
public ActionResult Index() {
return View(new SomeModel { date = DateTime.Now });
}
答案 2 :(得分: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>