c# - 提交表单后,值消失了

标签 c#

Before submits a form

提交表单后,“仅显示”值消失了, 有谁知道错误是什么?

After submits a form with error, this staff information disappears

在 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/

相关文章:

c# - 将用户页面添加到托管网站

javascript - 使用 View 动态访问模型

c# - 匹配所有有效格式 IPv6 地址的正则表达式

c# - 你如何从网络方法读取本地资源?

c# - 使用 LINQ to XML 使用文本和元素节点展平 xml

c# - 通过 oracle 中的存储过程使用 log4net

c# - 使用C#读取RAR文件的内容

c# - System.Text.Encoding.UTF8.GetBytes 额外字节

c# - 在 c# 日期和 sql 日期时间之间转换的简单方法

c# - 无法设置多重绑定(bind)