尝试重新发布 View ,但我想要的字段为空。
这是 Controller ...
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");
}
}
我的 View 中已经有一个 @Html.HiddenFor(model => Model.FileName) 。
但我仍然得到 Model.FileName 的空值。
请帮忙
谢谢
最佳答案
如果您打算在 POST 操作中修改 View 模型的某些值,您需要首先从 modelstate 中删除旧值:
ModelState.Remove("FileName");
vm.FileName = "2222";
这样做的原因是,Html 帮助程序(例如 TextBox、Hidden 等)在绑定(bind)时将首先使用模型状态中的值,然后再使用 View 模型中的值。
也可以代替:
@Html.HiddenFor(model => Model.FileName)
你应该使用:
@Html.HiddenFor(model => model.FileName)
注意表达式中的小写 m
。
关于asp.net-mvc-3 - 在 MVC 中的 Http Post 之后渲染相同的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9225039/