【发布时间】:2016-10-15 09:57:44
【问题描述】:
我想使用 hiddenFor 字段将 record_time 保存到数据库,同时在 html.display 或类似字段中显示日期而不进行编辑。
创建动作
public ActionResult Create()
{
Record newRecord = new Record();
newRecord.user_id = 1; //let it be 1 for simplicity
newRecord.record_time = System.DateTime.Now;
return View(newRecord);
}
创建视图
<div class="form-group">
@Html.LabelFor(model => model.user_id, "User name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.user_id)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.record_time, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.record_time)
//this line I'm concerned about
@Html.DisplayFor(model => model.record_time, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
-
关于 user_id。对新记录使用 hiddenFor 而不是 editorFor 是否正确?
-
关于记录时间。同时使用 hiddenFor 和 displayFor 是否正确?哪一个将用于将数据发送回数据库?仅显示预定义的日期时间信息并将其发送到数据库的正确方法是什么?
【问题讨论】:
-
只有表单控件回传一个值(
DisplayFor()不创建表单控件)。但是对于record_time,您根本不应该有表单控件-您只是在保存数据之前在控制器中设置。至于user_id- 如果你不想编辑它,那么它应该是一个隐藏输入
标签: asp.net asp.net-mvc html-helper html.hiddenfor