문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

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.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top