asp.net-mvc ASP.Net MVC - 从一个控制器发布到另一个(动作到动作)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2746094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:16:49  来源:igfitidea点击:

ASP.Net MVC - post from one controller to another (action to action)

asp.net-mvcpost

提问by AndreMiranda

Is is possible to do a postfrom an Action "Save" in a controller "Product" to an Action "SaveAll" in a controller "Category"??

是否可以做一个岗位在控制器“产品”到行动“白水”在控制器“类别”从行动“保存”?

And also passing a FormCollection as parameter

并且还传递一个 FormCollection 作为参数

回答by Amitabh

You can declare a form like this in your View and can specify whatever controller or Action you wish.

你可以在你的视图中声明一个这样的表单,并且可以指定你想要的任何控制器或操作。

Html.BeginForm("SaveAll", "Category", FormMethod.Post);

If you are in a controller then you can use.

如果您在控制器中,则可以使用。

TempData["Model"] = Model;
RedirectToAction("SaveAll", "Category");

回答by Robert Harvey

I would either just update your categories in your repository from your Product controller Save method directly, or refactor the Save Categories functionality in its own method, and call that from both controller methods.

我要么直接从您的 Product 控制器 Save 方法更新您存储库中的类别,要么在其自己的方法中重构 Save Categories 功能,并从两个控制器方法中调用它。

回答by Neil T.

public class Product : Controller
{
    ...
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Save(FormCollection productValues)
    {
        ...
        RedirectToAction("SaveAll", "Category", new { formValues = productValues });
    }
    ...
}

public class Category : Controller
{
    ...
    public ActionResult SaveAll(FormCollection formValues)
    {
        ...
    }
}

The assumption is that you are executing the POST in the context of the Product.

假设您正在产品的上下文中执行 POST。

回答by 3Dave

Since POSTis a verb for an HTTP request, this only makes sense (as written) if the .Save()method initiates an HTTP loopback connection to the appropriate .SaveAll(), (like http://..../Category/SaveAll) route and passes the form collection as part of the request. This is silly and not recommended, since this will break your ability to unit test this controller.

由于POST是 HTTP 请求的动词,因此只有当该.Save()方法启动到适当的.SaveAll(),(如http://..../Category/SaveAll)路由的 HTTP 环回连接并将表单集合作为请求的一部分传递时,这才有意义(如所写)。这很愚蠢,不推荐,因为这会破坏您对该控制器进行单元测试的能力

If, however, you mean that you want to call .SaveAll()and return its rendered result back to the client, you could use .RenderAction()and pass the model or form collection received by .Save()as the parameter.

但是,如果您的意思是要调用.SaveAll()并将其呈现的结果返回给客户端,则可以使用.RenderAction()并传递.Save()作为参数接收的模型或表单集合。

Or, on the server side, just instantiate the Category controller and call its .SaveAll() method, again passing the model received by .Save()as the parameter.

或者,在服务器端,只需实例化 Category 控制器并调用其 .SaveAll() 方法,再次传递.Save()作为参数接收的模型。

public ActionResult Save(MyModel m)
{
    Category cat = new Category();

    return cat.SaveAll(m);
}

However, you'll have to take the result from that call and make sure it's handled properly by the resulting view.

但是,您必须从该调用中获取结果,并确保结果视图对其进行了正确处理。

If this is what you're trying to do, it's worth noting that you should really have the code of the .SaveAll()method that performs the save separated into a dedicated business logic layer rather than living in the controller. All of this functionality should, in theory, be available for use in a different controller, or in a library that could be included in other applications.

如果这是您想要做的事情,那么值得注意的是,您确实应该将.SaveAll()执行保存的方法的代码分离到专用的业务逻辑层中,而不是放在控制器中。理论上,所有这些功能都应该可用于不同的控制器或可包含在其他应用程序中的库中。

回答by Maheshwari Kahar

return RedirectToAction("ViewService", "Admin")

return RedirectToAction("ViewService", "Admin")

Here, "ViewService" is an Action Name and "Admin" is Controller Name

这里,“ViewService”是动作名称,“Admin”是控制器名称