Asp.net-mvc – Add views in sub-folders in ASP.NET MVC 3

asp.net-mvcasp.net-mvc-3

I am working on ASP.NET MVC 3 project. I want to divide controllers, models, and views in sub-folders for the sake of simplicity. I able to do it with controllers and models but when i create a view it creates automatically to root folder Views, Is there any way to solve this problem?

eg. I can create

model class as,

Models/Finance/Bank.cs
Models/Finance/Finance.cs
Models/Production/Production.cs

controller as,

Controllers/Finance/BankController/Create
Controllers/Finance/BudgetController/Create
Controllers/Production/ProcessController/Create

but where i tried to create view for above actions, it creates in to,

Views/Bank/Create.aspx
Views/Budget/Create.aspx
Views/Process/Create.aspx

I want it should be like,

Views/Finance/Bank/Create.aspx
Views/Finance/Budget/Create.aspx
Views/Prodution/Process/Create.aspx

Is there any way to create views in same sub-folder as of that created for Controllers and models? thanks!

Best Answer

following steps worked for me,

  1. Create sub-folders as you want in Views (root folder). in my case it was Finance & Production.

  2. Simply drag automatically created folders in Views in to appropriate Sub-folders. in my case Bank & Budget in to Finance and Process in to Production

  3. While you return a view from controller action, give full path of view as,

    return View("~/Views/Finance/Bank/Create.aspx")

    return View("~/Views/Finance/Budget/Create.aspx")

    return View("~/Views/Production/Process/Create.aspx")

Related Topic