C# 提交表单后,该值消失

C# 提交表单后,该值消失,c#,C#,提交表单后,“仅显示”值消失, 有人知道错误是什么吗 在视图中,我使用@Html.textboxfor并使用ViewBag.StaffId值作为“readonly”变量,如果我提交表单时出现错误,ViewBag.StaffId值将消失 <div class="form-group"> @Html.LabelFor(model => model.StaffId, "StaffId", htmlAttributes: new { @class = "con

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

在视图中,我使用@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>

你最喜欢的是后绑定模型 比如说

public ActionResult myController(){
  var model= new myModel();
  return View(model);
}

[HttpPost]
public ActionResult myController(myModel model){
  model.StaffId = 1;
  return View(model);
}

你最喜欢的是后绑定模型 比如说

public ActionResult myController(){
  var model= new myModel();
  return View(model);
}

[HttpPost]
public ActionResult myController(myModel model){
  model.StaffId = 1;
  return View(model);
}

您没有发布控制器代码。无论如何,试试这个例子

[HttpPost]         
public ActionResult <Resultnamehere>(<YOUR MODEL CLASS HERE> model)
{

  return View(Model);
}
[HttpPost]
公共行动结果(模型)
{
返回视图(模型);
}

您没有发布控制器代码。无论如何,试试这个例子

[HttpPost]         
public ActionResult <Resultnamehere>(<YOUR MODEL CLASS HERE> model)
{

  return View(Model);
}
[HttpPost]
公共行动结果(模型)
{
返回视图(模型);
}

为了获得更好的结果,请为编程语言添加一个标记在控制器的post方法中有返回视图()之前的部分可能会更好,但据我所见,这可能是因为在post方法中,不会重新填充ViewBag变量。您需要在每个返回视图之前设置它们。为了获得更好的结果,请添加编程语言的标记。最好在控制器的post方法中包含返回视图()之前的部分,但从我看到的情况来看,这可能是因为在post方法中,不会重新填充ViewBag变量。您需要在每次返回视图之前设置它们。我的控制器代码:[HttpPost][ValidateAntiForgeryToken]公共操作结果创建([Bind(Include=“StaffId,Tdate,Edate”)]TrainingRecord TrainingRecord{if(ModelState.IsValid){db.TrainingRecordDBSet.Add(TrainingRecord);db.SaveChanges();返回重定向到操作(“搜索”,“训练记录”,新建{id=trainingRecord.StaffId});}返回视图(trainingRecord);}我的控制器代码:[HttpPost][ValidateAntiForgeryToken]公共操作结果创建([Bind(Include=“StaffId,Tdate,Edate”)]trainingRecord训练记录){if(ModelState.IsValid){db.TrainingRecordDBSet.Add(trainingRecord);db.SaveChanges();返回重定向到操作(“搜索”,“TrainingRecords”,new{id=trainingRecord.StaffId});}返回视图(trainingRecord);}我的控制器代码:[HttpPost][ValidateAntiForgeryToken]公共操作结果创建([Bind(Include=“StaffId,Tdate,Edate”)]trainingRecord trainingRecord trainingRecord){if(ModelState.IsValid){db.TrainingRecordDBSet.Add(trainingRecord);db.SaveChanges();返回重定向到操作(“搜索”,“TrainingRecords”,new{id=trainingRecord.StaffId});}返回视图(trainingRecord);}我的控制器代码:[HttpPost][ValidateAntiForgeryToken]公共操作结果创建([Bind(Include=“StaffId,Tdate,Edate”)]TrainingRecord TrainingRecord{if(ModelState.IsValid){db.TrainingRecordDBSet.Add(TrainingRecord);db.SaveChanges();返回重定向到操作(“搜索”,“TrainingRecords”,new{id=TrainingRecord.StaffId});}返回视图(TrainingRecord);}