roncansan
roncansan

Reputation: 2380

Problem returning specific view in asp.net mvc3

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.

Upvotes: 1

Views: 9561

Answers (4)

Vadim
Vadim

Reputation: 17955

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.

Upvotes: 1

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.

Upvotes: 2

Moble Joseph
Moble Joseph

Reputation: 657

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

Upvotes: 1

Jamie Dixon
Jamie Dixon

Reputation: 54011

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.

Upvotes: 7

Related Questions