【发布时间】:2014-05-02 08:31:51
【问题描述】:
在 ASP.NET MVC 5 中,是否每个 Controller Action 都必须返回与 Controller 同名的 View?
这是我的项目。有一个网页,其中包含一个将图像上传到数据库的按钮。加载网页时,我希望它显示已上传的所有图像的列表。因此,该控制器的索引(默认)操作从数据库中加载图像,并返回索引视图,该视图依次显示图像列表:
public ActionResult Index()
{
// Load the images from the database
var images = GetImages();
return View(images);
}
在同一个网页上,有一个按钮允许用户将图像上传到数据库。该按钮调用上传操作,它根据传递的“文件”和“文件夹”参数上传文件,然后最终再次返回索引视图:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file, string folder)
{
// Upload the file from the specified folder
// ...
// ...
// ...
return Index();
}
但是,当用户单击此上传按钮时,会显示以下错误消息:
The view 'Upload' or its master was not found or no view engine supports the searched locations
但我并没有尝试渲染名为“Upload”的视图 - 我正在尝试渲染名为“Index”的视图,这就是我使用 return Index(); 行的原因。
对我哪里出错有帮助吗?
【问题讨论】:
-
在 POST 方法之后重定向用户是个好主意。看看Post/Redirect/Get。
标签: asp.net-mvc