Problem returning specific view in asp.net mvc3
-
25-09-2019 - |
Question
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.
Solution
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.
OTHER TIPS
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.