Rajan Rawal
Rajan Rawal

Reputation: 6313

How to make module base structure for asp .net mvc 3 project?

I want to create module based structure, Like in zend framework. Suppose I have 2 controllers like student and teacher and I want to put all these in one folder say shchool. Same way I want the view files for each controller in school folder for e.g

For Controller:

D:\aspprojects\Project1\Project1\Controllers\School\TeacherController.cs
D:\aspprojects\Project1\Project1\Controllers\School\StudentController.cs

and for view files:

D:\aspprojects\Project1\Project1\Views\School\Teacher\all CRUD files(*.cshtml)
D:\aspprojects\Project1\Project1\Views\School\Student\all CRUD files(*.cshtml)

Current structure is like as below,

for Controllers:

D:\aspprojects\Project1\Project1\Controllers\TeacherController.cs
D:\aspprojects\Project1\Project1\Controllers\StudentController.cs

For view files

D:\aspprojects\Project1\Project1\Views\Teacher\all CRUD files(*.cshtml)
D:\aspprojects\Project1\Project1\Views\Student\all CRUD files(*.cshtml)

What changes do I need to do?

Upvotes: 0

Views: 313

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

The problem you are facing is that MVC doesn't care what folder the controller is in. In fact, it doesn't even have to be in a folder called Controllers. MVC only looks for classnames with Controller in the name. Once compiled, the folder structure is lost, and as such, no way for the framework to know to look in a subfolder for a view, because that information is no longer present in the compiled code.

You can still do it, however.. but you can no longer rely on MVC to find your view files automatically, you will have to pass each view name directly.

This means you will have to do this:

return View("~/Views/School/Teacher/Index.cshtml");

Rather than this.

return View();

Another option is to use areas, which allows you to create a School area, and then you can create a teacher and student controllers within the school area.

Upvotes: 2

Related Questions