从另一个控制器调用一个动作

时间:2022-12-01 22:48:44

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.

我有两个不同的对象:合同和任务订单。我的要求指定为了查看任一对象的详细信息,Url应该是“http://.../Contract/Details”或“http://.../TaskOrder/Details”,具体取决于哪种类型。它们非常相似,细节页几乎相同,所以我创建了一个可以是契约或任务顺序的类,并且有一个变量“objectTypeID”,它说明它是哪种类型。我在任务顺序控制器中编写了“详细信息”操作,但现在我想从合同控制器调用它而不是重新复制代码。

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

那么有没有办法让网址仍然说“... / Contract / Details”,而是调用TaskOrder控制器中的动作呢?我试过用

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.

除了我不能再使用HttpContext.Session之外,这本来有用,我在动作中多次使用过。

4 个解决方案

#1


1  

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.

如果您有两个独立控制器使用的公共代码,那么您应该将此代码抽象到另一个类。

#2


0  

RedirectToAction("Details","To");

RedirectToAction( “详细信息”, “要”);

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.

此外,也许您需要一个BaseController类,这两个控制器继承并实现相同的Details操作,但基于objectTypeID执行稍微不同的操作。

#3


0  

Create a base class for the controller. Like DetailsController

为控制器创建基类。像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

将您的详细信息代码放在那里,让它接受typeId。然后让你的两个控制器派生自该基类,并使其Details操作调用传入id的基类

#4


0  

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

谢谢大卫,我应该从视角来称呼它。

All I needed was the following line in my Contract/Details.aspx page:

我需要的只是我的Contract / Details.aspx页面中的以下行:

<%= Html.Action("Details", "TaskOrder", new { id = ViewData["id"] })%>

<%= Html.Action(“Details”,“TaskOrder”,new {id = ViewData [“id”]})%>

#1


1  

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.

如果您有两个独立控制器使用的公共代码,那么您应该将此代码抽象到另一个类。

#2


0  

RedirectToAction("Details","To");

RedirectToAction( “详细信息”, “要”);

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.

此外,也许您需要一个BaseController类,这两个控制器继承并实现相同的Details操作,但基于objectTypeID执行稍微不同的操作。

#3


0  

Create a base class for the controller. Like DetailsController

为控制器创建基类。像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

将您的详细信息代码放在那里,让它接受typeId。然后让你的两个控制器派生自该基类,并使其Details操作调用传入id的基类

#4


0  

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

谢谢大卫,我应该从视角来称呼它。

All I needed was the following line in my Contract/Details.aspx page:

我需要的只是我的Contract / Details.aspx页面中的以下行:

<%= Html.Action("Details", "TaskOrder", new { id = ViewData["id"] })%>

<%= Html.Action(“Details”,“TaskOrder”,new {id = ViewData [“id”]})%>