AspNet MVC - 返回RedirectToAction不与图片上传(如果file.SaveAs被调用)
问题描述:
return RedirectToAction
不工作,如果imageUpload.SaveAs(path);
执行否则它工作(我的意思是如果我不选择任何图像,它不会到达线imageUpload.SaveAs(path);
)。AspNet MVC - 返回RedirectToAction不与图片上传(如果file.SaveAs被调用)
这里是我的代码:
[HttpPost]
public ActionResult CreateNewEmployee(Employee emplView, HttpPostedFileBase imageUpload)
{
if (!ModelState.IsValid)
{
var mod = new PersonalDetailsViewModel(emplView);
return View("AddEmployee", mod);
}
if (imageUpload != null && imageUpload.ContentLength > 0)
{
var fileName = emplView.EmployeeId + "_" + Path.GetFileName(imageUpload.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Images/"), fileName);
imageUpload.SaveAs(path);
emplView.Photograph = fileName;
}
_dbContext.Employees.Add(emplView);
_dbContext.SaveChanges();
return RedirectToAction("PersonalDetails", new { id = emplView.EmployeeId });
}
我与调试检查,它在到达最后一行,并执行而不是重定向到的操作方法。我不知道我在做什么错?
我内以下的Global.asax
方法对于任何隐藏的错误校验,但没有任何:
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
}
该异常可能发生在ASP.NET管道之外,所以不能保证Application_Error会捕获它。尝试将保存图像代码包装在try/catch中并/或将ELMAH添加到项目中。 –
现在发生了什么?你实际得到了什么结果?是否有例外? – Shyju
RedirectToAction之后 - 无论您是否能够调试“PersonalDetails”方法,或者它只是终止? – Rex