不確定如何更好地表達問題。我希望使用hiddenFor字段將record_time保存到數據庫,同時在html.display中顯示日期或類似於該字段而不進行編輯。在View中正確顯示數據並使用HiddenFor和DisplayFor保存它
CREATE動作
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);
}
CREATE VIEW
<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>
質詢
1)關於USER_ID。是否正確使用hiddenFor新記錄,而不是editorFor?
2)關於record_time。使用hiddenFor和displayFor是否正確?哪一個將用於將數據發回數據庫?只顯示預定義的日期時間信息並將其發送到數據庫的正確方法是什麼?
只有表單控件回發一個值('DisplayFor()'不會創建表單控件)。但是對於'record_time',你根本不應該有一個表單控制 - 你只需在控制器中設置,然後再保存數據。至於'user_id' - 如果你不想編輯它,那麼它應該是一個隱藏的輸入 –
@StephenMuecke,非常感謝。偉大的建議,以保存在HttpPost行動。我相應地糾正了,現在它完美了。 – SWA