我提交表单后,只显示""价值消失了, 谁能知道错误是什么?
在View中,我使用@ Html.textboxfor并使用ViewBag.StaffId值作为" readonly"变量,如果我提交带有错误的表单,则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>
控制器代码:
[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);
}
答案 0 :(得分:3)
你发布后的大多数绑定模型 例如
public ActionResult myController(){
var model= new myModel();
return View(model);
}
[HttpPost]
public ActionResult myController(myModel model){
model.StaffId = 1;
return View(model);
}
答案 1 :(得分:1)
您没有发布控制器代码。无论如何尝试这个例子
[HttpPost]
public ActionResult <Resultnamehere>(<YOUR MODEL CLASS HERE> model)
{
return View(Model);
}
答案 2 :(得分:0)
谢谢!在你的帮助之后我找到了答案!
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "StaffId, Tdate, Edate, Place, Organizer, Tname, Hour, Coefficient, Type, Certificate, CamMoney, CertPath, Year")] TrainingRecord trainingRecord)
{
if (ModelState.IsValid)
{
db.TrainingRecordDBSet.Add(trainingRecord);
db.SaveChanges();
return RedirectToAction("Search", "TrainingRecords", new { id = trainingRecord.StaffId });
}
ViewBag.StaffId = trainingRecord.StaffId;
return View(trainingRecord);
}