我的Index.cshtml
@model ReportGenerator.WebUI.Models.ReportViewModel
@Html.TextBoxFor(m => m.report.FileName, new { @class = "form-control", id = "FileName" })
我的 Controller
public ActionResult Index(ReportViewModel model)
{
...some stuff
model.report = new Report();
model.report.FileName = "INDEX";
return View(model);
}
public ActionResult fillFields(ReportViewModel _model)
{
...some stuff
_model.report = new Report();
_model.report.FileName = "FILL";
return View("Index", _model);
}
当我运行应用程序时,
TextBox
Text
属性设置为“INDEX”。同样,当我单击调用fillFields
Controller Action 的按钮时,TextBox
仍显示“INDEX”,它没有更改为“FILL”。我究竟做错了什么?为什么它不想工作?
最佳答案
@StephenMuecke在上面的评论中正确回答了这个问题。
您没有做错任何事。您的fillFields()
方法具有参数ReportViewModel,因此在初始化该方法时会将其值添加到ModelState。当您返回 View 时,TextBoxFor()
方法使用ModelState
中的值(不是model属性)来设置文本框的值。其原因在here中进行了说明。正确的方法是遵循PRG模式–
关于c# - MVC传递模型进行查看,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32944834/