Vman
Vman

Reputation: 3136

MVC Nested Controllers and Views

Some of our controllers are getting too large, so I am trying to simplify them by refactoring along the lines of:

Areas
    Vehicles
        Controllers
            Cars
                CarController.cs
                Engines
                    PistonController.cs
                    ...
        Models
        Views
            Cars
                EditCar.ascx
                Engines
                    EditPiston.ascx
                    ShowPiston.ascx
    Animals
    ...

(with namespaces matching the folder structure)

I've tried a few variations but can't get the MVC inference to recognise my project structure.

What does the partial view return look like in the action? Ie, EditPiston action with a simple return PartialView(piston) wants to use a view Views-> Engines -> EditPiston.ascx, not Views-> Cars->Engines -> EditPiston.ascx.

Also, what do I need to do in the RegisterArea? Any specific routes I need to set up?

Upvotes: 3

Views: 3595

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

Out of the box, you can't do this using standard conventions.

You can, of course, simply specify the views locations directly. ie:

return View("~/Areas/Views/Vehicles/Cars/Engines/Piston.cshtml");

Another option would be to create your own View Engine derived from RazorViewEngine which adds more search paths, however you should be aware that these paths will be search for each page render, and can significantly decrease performance if you have a lot of search paths.

An example of creating your own ViewEngine is here:

Can I specify a custom location to "search for views" in ASP.NET MVC?

Upvotes: 1

Related Questions