1

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.

2
  • 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 Commented Jan 5, 2011 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. Commented Jul 19, 2013 at 10:06

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

1 Comment

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
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.