【发布时间】:2015-03-25 16:51:14
【问题描述】:
我有一个带有文件上传控制的表单,它按预期工作。但是,在单击提交按钮并从控制器返回模型以防页面上出现其他一些无效字段后,我无法弄清楚如何“保存”选定的文件? 每次页面出现错误时,用户都必须再次浏览文件。在控制器上调用 POST 后保存选择的最佳做法是什么?
//VIEW
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input id="inputbox" type="file" name="PageUpload" />
@Html.TextBoxFor(model => model.otherField1)
@Html.TextBoxFor(model => model.otherField2)
}
//CONTROLLER
[HttpPost]
public ActionResult Create(MyViewModel view)
{
try
{ ...
if (ModelState.IsValid)
{
//it is valid save
return RedirectToAction("Action", "Controller", new { id = view.ModelID });
}
}
catch(Exception e){}
return View(view);
}
在返回 View(view) 而不是输入控件后,我得到了正确绑定的所有其他字段。
【问题讨论】: