试图重新发布一个视图,但我得到null我想要的字段. 继承人控制器…… public ActionResult Upload() { VMTest vm = new VMTest(); return View(vm); } [HttpPost] public ActionResult Upload(VMTest vm, String submitButton) {
继承人控制器……
public ActionResult Upload() { VMTest vm = new VMTest(); return View(vm); } [HttpPost] public ActionResult Upload(VMTest vm, String submitButton) { if (submitButton == "Upload") { //do some processing and render the same view vm.FileName = "2222"; // dynamic creation of filename vm.File.SaveAs(@vm.FileName); // save file to server return View(vm); } else if (submitButton == "Save") { //read the file from the server FileHelperEngine engine = new FileHelperEngine(typeof(PaymentUploadFile)); PaymentUploadFile[] payments = (PaymentUploadFile[])engine.ReadFile(@vm.FileName); // the problem lays here @vm.FileName has no value during upload //save the record of the file to db return View("Success"); } else { return View("Error"); } }
我的视图中已经有一个@ Html.HiddenFor(model => Model.FileName).
但我仍然得到Model.FileName的空值.
任何帮助请
谢谢
如果您打算在POST操作中修改视图模型的某些值,则需要先从modelstate中删除旧值:ModelState.Remove("FileName"); vm.FileName = "2222";
原因是TextBox,Hidden,…等Html帮助器在绑定时首先使用modelstate中的值,然后在视图模型中使用值.
而不是:
@Html.HiddenFor(model => Model.FileName)
你应该使用:
@Html.HiddenFor(model => model.FileName)
注意表达式中的小写m.