MVC无法在ActionMethod中找到View

Lal*_*cky 2 asp.net-mvc-3

刚开始使用MVC,我遇到了问题.以下是我的文件的层次结构:

1.WebJltNZ\JWebJltNZ.Presentation.Web.Mvc\Controllers : LbpProfessionalController
2.WebJltNZ.Presentation.Web.Mvc\ViewModels : LbpProfessional
3.WebJltNZ.Presentation.Web.Mvc\Views\Home\RiskAndInsuranceServices\JltAffinityPartnerships            :LbpProfessionalProtectionApplication
Run Code Online (Sandbox Code Playgroud)

方法如下:

public ActionResult Index()
{
    return View(); // it's cannot be found.
}
Run Code Online (Sandbox Code Playgroud)

无法找到该视图.

我在这里错过了一些东西.请帮忙.

Ohl*_*lin 6

你的名字错了.它的工作方式是控制器名称匹配视图文件夹中的子文件夹; 并且action方法匹配该子文件夹中的文件.

这意味着,LbpProfessionalController在控制器文件夹应该命名文件夹匹配LbpProfessional里面Views的文件夹.

并且Index里面的方法LbpProfessionalController应匹配Index.cshtml文件\Views\LbpProfessional夹内的文件.

那么结构就像这样

\Controllers\LbpProfessionalController.cs
\Views\LbpProfessional\Index.cshtml
Run Code Online (Sandbox Code Playgroud)

请注意,控制器的名称以...结尾,...Controller但文件夹名称不会获得该部分.

这是链接控制器和视图的标准方法,当您遵循这些规则时,您可以使用一个简单的操作方法:

public ActionResult Index()
{
   // This view will be found if you have given the view the right name 
   // ("Index.cshtml") and put it in the right place (folder named 
   // after controller).
   return View(); 
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您希望视图与默认的链接方式不同,则需要指定其他视图的路径.它可能看起来像这样:

public ActionResult Index()
{
   return View("anotherViewName"); 
}
Run Code Online (Sandbox Code Playgroud)