Reputation: 457
Suppose I am working on a Car Portal. To search a new car, the user provides the following information:
Business Rules:
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
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
ListForFuleAndBudget(string fuelType,string budget)
List(string brandName)
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
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