在 asp.net mvc3 中返回特定视图时出现问题

发布于 2024-10-10 23:53:41 字数 1145 浏览 7 评论 0原文

我有一个像这样的视图文件结构:

Views
   Company
      Department
      Employee
          ManageEmployee.cshtml

控制器是

public class EmployeeController : Controller
 {
    public ActionResult Index(int dptId)
    {
            var loadedEmp = getEmpOf(dptId);
            return View("Company/Employee/ManageEmployee", loadedEmp);
     }
}

但是控制器给了我一个错误 - 告诉我它找不到视图。这些是它搜索的路径。

~/Views/Employee/Company/Employee/ManageEmployees.aspx
~/Views/Employee/Company/Employee/ManageEmployees.ascx
~/Views/Shared/Company/Employee/ManageEmployees.aspx
~/Views/Shared/Company/Employee/ManageEmployee.ascx
~/Views/Employee/Company/Employee/ManageEmployee.cshtml
~/Views/Employee/Company/Employee/ManageEmployee.vbhtml
~/Views/Shared/Company/Employee/ManageEmployee.cshtml
~/Views/Shared/Company/Employee/ManageEmployee.vbhtml

基本上,如果我能够消除 Employee 部分,引擎就会找到它。

~/Views/Employee/Company/Employee/ManageEmployee.cshtml 对此

~/Views/Company/Employee/ManageEmployee.cshtml

有关如何实现此目标的任何见解。

谢谢。

I have a view file structure like:

Views
   Company
      Department
      Employee
          ManageEmployee.cshtml

and the controller is

public class EmployeeController : Controller
 {
    public ActionResult Index(int dptId)
    {
            var loadedEmp = getEmpOf(dptId);
            return View("Company/Employee/ManageEmployee", loadedEmp);
     }
}

But the controller give me an error - telling that it can't find the view.These are the paths it search.

~/Views/Employee/Company/Employee/ManageEmployees.aspx
~/Views/Employee/Company/Employee/ManageEmployees.ascx
~/Views/Shared/Company/Employee/ManageEmployees.aspx
~/Views/Shared/Company/Employee/ManageEmployee.ascx
~/Views/Employee/Company/Employee/ManageEmployee.cshtml
~/Views/Employee/Company/Employee/ManageEmployee.vbhtml
~/Views/Shared/Company/Employee/ManageEmployee.cshtml
~/Views/Shared/Company/Employee/ManageEmployee.vbhtml

Basically if I'm able to eliminate the Employee section, the engine will find it.

~/Views/Employee/Company/Employee/ManageEmployee.cshtml to this

~/Views/Company/Employee/ManageEmployee.cshtml

Any insights on how to achieve this.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月下凄凉 2024-10-17 23:53:41

您是否尝试过:

return View("/Company/Employee/ManageEmployee", loadedEmp);

看起来引擎正在尝试返回相对于您在站点中当前位置的视图,而不是从站点的根目录返回视图。

Have you tried:

return View("/Company/Employee/ManageEmployee", loadedEmp);

It looks like the engine is trying to return the view relative to your current location in the site rather than from the root of the site.

失与倦" 2024-10-17 23:53:41

视图必须通过以下方式从控制器返回(对于特定视图):

return View("ManageEmployee", loadedEmp);

在 MVC 中,控制器将自动路由到您提供的视图名称。

loadedEmp 应该是您传递给视图的对象。

View has to be returned from the controller in the following way (for Specific View):

return View("ManageEmployee", loadedEmp);

In MVC, the controller will automatically route to the View name you provided.

loadedEmp should be the object you are passing to the view.

回首观望 2024-10-17 23:53:41

您需要遵循控制器的 MVC 约定 ControllerNameController 以及 ControllerName/ 的视图结构...

如果您想完全控制您的结构,您需要切换到不同的框架,例如 FubuMVC

You need to follow MVCs convention of ControllerNameController for your controller and your view structure of ControllerName/...

If you want full control over your structure you'll need to switch to a different framework like FubuMVC.

独自←快乐 2024-10-17 23:53:41

如果您想要自己的视图文件夹结构安排惯例,最好插入您自己的视图引擎。

If you want your own convention of arranging the views folder structures, it would be better you plug in your own view engine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文