Paul
Paul

Reputation: 457

How to organize controller class and action method?

Suppose I am working on a Car Portal. To search a new car, the user provides the following information:

Business Rules:

  1. If user selects Brand only : Show brand list page with all models of selected brand
  2. If user selects Brand and Model : Show Model List page
  3. If user selects Brand + Model + Model version : Show model version details page.
  4. if user selects Fuel type OR Budget : Show Brand list page.

Now I have two approaches to define controller and action.

Approach 1: One Controller class with multiple action method

Controller Class : CarSearchmanager
with following are Action Methods:
        - SearchNewCar(int Barnd,int Model.......)
          Depending on user selection this method will redirect control to following  
          action method:

        - BrandListing(int BrandID......)
        - ModelListing(int BrandID,intModelID.....)
        - ModelVersionDetails((int BrandID,intModelID,int ModelVersionID....)

Approach 2: Multiple controller class

Controller Class : CarSearchmanager

Following are Action Methods:
        - SearchNewCar(int Barnd,int Model.......)
          Depending on user selection this method will redirect control to following 
          controller action method:

Then I will have separate controller class and action method for each of the pages like 
bellow:

    - BrandListing
    - ModelListing
    - ModelVersionDetails

I am very confused about how to organize the controller class and action methods. Is there any best practice kind of document? Please suggest one to me.

Upvotes: 1

Views: 637

Answers (2)

Shyju
Shyju

Reputation: 218732

I do not think there is a defined best version. Define it in such a way that you feel more clean and organized. From my understanding of your requirement, i may define like this

BrandController

action methods

  • ListForFuleAndBudget(string fuelType,string budget)
  • List(string brandName)

ModelController

action method

  • List(string brand,string model)
  • Details(string brand, string model, string version)

Now if you want nice url where you do not want the action method names as ot is, (ex :details) you can define your pretty urls when registering your routes in global.asax, before the generic route definition.

routes.MapRoute("list", "model/{brand}/{model}",
               new { controller = "brand", action = "List");

routes.MapRoute("list", "model/{brand}/{model}/{version}",
               new { controller = "brand", action = "details");

//default route definition goes here

Now yoursitename/model/honda/camry will take the user to the list action method and yoursitename/model/honda/camry/lx will take them to the details action method.

Upvotes: 1

Cheranga
Cheranga

Reputation: 125

my suggestion is to have this in the same controller Since this is a search functionality which will be used In a similar context. Use method overloading and in the Corresponding methods return the particular view.

Upvotes: 0

Related Questions