views:

71

answers:

4

I have two different objects: contracts, and task orders. My requirements specify that in order to view the Details for either object, the Url should be "http://.../Contract/Details" or "http://.../TaskOrder/Details" depending on which type. They are both very similar and the details pages are almost identical, so I made a class that can either be a contract or a task order, and has a variable "objectTypeID" that says which type it is. I wrote the action "Details" in the task order controller, but now I want to call that from the contract controller instead of recopying the code.

So is there any way to have the url still say ".../Contract/Details" but call the action in the TaskOrder controller instead? I tried using

TaskOrderController TOController = new TaskOrderController();
TOController.Details(id);

This would have worked except that I can't use the HttpContext.Session anymore, which I used several times in the action.

+2  A: 

Why are you calling a controller from a controller? A controller action should be called via a route and return a view.

If you have common code used by two separate controllers then you should be looking to abstract this code to another class.

David Neale
Views shouldn't call controllers; they should callroutes.
David Lively
Good point. Amended.
David Neale
A: 

RedirectToAction("Details","To");

In addition, add routing parameters if you need to.

Also, maybe you need a BaseController class which these two controllers inherit from and which implement the same Details action, but based on the objectTypeID do slightly different things.

Wim Hollebrandse
That would work, but then it changes the URL to say "TaskOrder/Details" instead of "Contract/Details"
Brian
Well, the Details action is in the TaskOrder controller. Maybe it doesn't logically belong there? Should you have a ContractController with a Details action on perhaps?
Wim Hollebrandse
A: 

Thanks David, I should be calling it from the view.

All I needed was the following line in my Contract/Details.aspx page:
\<%= Html.Action("Details", "TaskOrder", new { id = ViewData["id"] })%>

Brian
A: 

Create a base class for the controller. Like DetailsController

Put your details code in there, and have it accept an typeId. Then have your two controllers derive from that base class, and have their Details action call the base class passing in the id

taylonr