提交表单后,“仅显示”值消失了, 有谁知道错误是什么?
在 View 中,我使用@Html.textboxfor 并将 ViewBag.StaffId 值用作“只读”变量,如果我提交有错误的表单,ViewBag.StaffId 值将消失
<div class="form-group">
@Html.LabelFor(model => model.StaffId, "StaffId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.StaffId, new { @Value = ViewBag.StaffId, @readonly = "readonly", @class = "form-control" })
@Html.ValidationMessageFor(model => model.StaffId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StaffInfo.Cname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.StaffInfo.Cname, new { @Value = ViewBag.CName, @readonly = "readonly", @class = "form-control" })
@Html.ValidationMessageFor(model => model.StaffInfo.Cname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tdate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tdate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Tdate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Edate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Edate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Edate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StaffId, "StaffId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.StaffId, new { @Value = ViewBag.StaffId, @readonly = "readonly", @class = "form-control" })
@Html.ValidationMessageFor(model => model.StaffId, "", new { @class = "text-danger" })
</div>
</div>
Controller 代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "StaffId, Tdate, Edate")] TrainingRecord trainingRecord)
{
if (ModelState.IsValid)
{
db.TrainingRecordDBSet.Add(trainingRecord);
db.SaveChanges();
return RedirectToAction("Search", "TrainingRecords", new { id = trainingRecord.StaffId });
}
return View(trainingRecord);
}
最佳答案
你在帖子后最绑定(bind)模型 例如
public ActionResult myController(){
var model= new myModel();
return View(model);
}
[HttpPost]
public ActionResult myController(myModel model){
model.StaffId = 1;
return View(model);
}
关于c# - 提交表单后,值消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43066470/