1

I've been researching about this. I found "MVCs areas" but I still couldn't do what I'm looking for.

MY VIEWS: Views/ Student/Course Student/Information Student/Status Student/GeneralSituation etc, etc...

CONTROLLERS:

Controllers/Student

What I want to do is:

I don't want to have All the code from a lot of views in just one "Student" controller. Any tip regarding how I can "split" my controllers in several files? I'm just looking at the simplest approach, I don't want to make big modifications to my project.

I'm using MVC4.

Thanks in advance!..

PnP

3 Answers 3

2

Why not just make partial Controller classes and thus split one controller over a bunch of physical files?

Also, what do you mean by "the code from a lot of views"? Are you using a separate service layer and doing the business logic in there, because that's best practice. Controllers are meant to be very lightweight with code along the lines of this:

public ActionMethod DoSomething()
{
    StudentViewModel vm = _studentService.GetSomeData();
    return View(vm);
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can do a partial class StudentController:

your folder/files would look like this:

Controllers
  StudentController
    StudentController.Status.cs
    StudentController.GeneralSituation.cs
    StudentController.Course.cs

The code would be:

StudentController.Status.cs:

public partial class StudentController
{
    [Actions relevant for Status of a student]
}

StudentController.GeneralSituation.cs:

public partial class StudentController
{
    [Actions relevant for General Situation of a student]
}

Comments

0

Any reason why Area's don't work? From what you described, I don't really see why they wouldn't.

- Areas
    - Students
      - Controllers
          HomeController           Handles base /Students/ route
          InformationController    ~/Students/Information/{action}/{id} 
          StatusController         ~/Students/Status/{action}/{id}
          ...
      - Models
      - Views
          Home/
          Information/
          Status/
          ...
          Shared/        Stick common views in here

If you're set on one monster controller (or partials), your controller should have very little actual 'View code' in it. Leave all that to view models - the controller just passes in the needed resources to build view data, keeping controllers thin.

Ie,

public class StudentController
{
    ...

    // Actually I prefer to bind the id to a model and handle 404
    // checking there, vs pushing that boiler plate code further down 
    // into the controller, but this is just a quick example.

    public ActionResult Information(int id)
    {
        return View(new InformationPage(this.StudentService, id));
    }
}

Then, InformationPage is one of your models that will handle building out all information applicable to that view.

public class InformationPage
{
     public Student Student { get; set; }

     public InformationPage(StudentService service, int studentId)
     {
         Student = service.FindStudent(studentId);
         ... Other view data ...
     }
}

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.