Asp.net mvc 3 MVC无法在ActionMethod中找到视图
刚开始使用MVC,我遇到了一个问题。 以下是我的文件的层次结构:Asp.net mvc 3 MVC无法在ActionMethod中找到视图,asp.net-mvc-3,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\JltAff
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
方法如下:
public ActionResult Index()
{
return View(); // it's cannot be found.
}
找不到视图
我是不是遗漏了什么。请帮忙。你的名字搞错了。其工作方式是控制器名称与视图文件夹中的子文件夹匹配;操作方法与该子文件夹中的文件匹配 这意味着控制器文件夹中的
LbpProfessionalController
应与Views
文件夹中名为LbpProfessional
的文件夹匹配
LbpProfessionalController
中的Index
方法应与\Views\LbpProfessional
文件夹中的Index.cshtml
文件匹配
然后结构会像这样
\Controllers\LbpProfessionalController.cs
\Views\LbpProfessional\Index.cshtml
请注意,控制器的名称以…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();
}
但是,如果希望视图与默认链接方式不同,则需要指定指向该其他视图的路径。它可能是这样的:
public ActionResult Index()
{
return View("anotherViewName");
}
将控制器类继承到您放置了public ActionResult Index()的类中。视图应位于
/Views/LbpProfessional/Index.cshtml
。在那里吗?不在那里。。它位于WebJltNZ.Presentation.Web.Mvc\Views\Home\RiskAndInsuranceServices\JltAffinityPartnerships\LbpProfessionalProtectionApplication.cshtml.下。。这就是问题所在吗?是的。这就是你的“问题”。MVC是按约定工作的-它无法“知道”视图的位置:您需要使用约定,或指定视图的路径。它必须在@Kobi告诉您的位置,否则您必须将视图的位置传递给view():返回视图(“~/Views/Home/risk和insuranceservices/JltAffinityPartnerships/LbpProfessionalProtectionApplication”)
我曾经遇到过一些模板的情况,在这些模板中,通过控制器添加文件夹会导致视图引擎找不到它们。谢谢Ohlin。它确实帮助了我,解决了这个问题。另外,对于像我这样的新手来说,你的信息太好了。快乐博客:)我很高兴它对你有效。如果你对答案感到满意,那么也请标记我的答案SWER是正确的。祝你所有的未来MVC冒险:)在我的答案的左手边,如果你喜欢答案,你可以上下投票。在这个投票部分,有一个V形图形,你可以点击你认为最好的答案(如果很多人已经回答了你的问题)。.然后,该数字将变为绿色,表示这是您接受的答案。