Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
A bit irrelevant/slightly related, but maybe consider using T4MVC? This will avoid magic strings. It definitely helps when manually entering view names - mvccontrib.codeplex.com/wikipage?title=T4MVC –  Dan Atkinson Jan 5 '11 at 21:02
 
You have two options here Option #1 Create the Company/Department/Employee Directory inside Shared Folder. You can locate the Shared Folder inside the View Folder of Root Directory. Option #2 Create the Employee/Company/Department/Employee Directory indide View Directory of your Root Folder. –  PKKG Jul 19 at 10:06

4 Answers

up vote 1 down vote accepted

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.

share|improve this answer
1  
Absolutely, I need to follow MVCs convention. What I did to resolve my case was to create partial classes of the same controller with different file name,to separate the logic, but have the URl structure I was looking for. Thanks –  roncansan Jan 5 '11 at 19:17

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.