不同的控制器操作方法之间传递数据 [英] Passing data between different controller action methods

查看:111
本文介绍了不同的控制器操作方法之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 ASP.NET MVC 4 。我试图从一个控制器数据传递到另一个控制器。我没有得到这个权利。我不知道这是可能的?

I'm using ASP.NET MVC 4. I am trying to pass data from one controller to another controller. I'm not getting this right. I'm not sure if this is possible?

下面是我想从数据传递我的源操作方法:

Here is my source action method where I want to pass the data from:

public class ServerController : Controller
{
     [HttpPost]
     public ActionResult ApplicationPoolsUpdate(ServiceViewModel viewModel)
     {
          XDocument updatedResultsDocument = myService.UpdateApplicationPools();

          // Redirect to ApplicationPool controller and pass
          // updatedResultsDocument to be used in UpdateConfirmation action method
     }
}

我需要将它传递给该操作方法在此控制器:

I need to pass it to this action method in this controller:

public class ApplicationPoolController : Controller
{
     public ActionResult UpdateConfirmation(XDocument xDocument)
     {
          // Will add implementation code

          return View();
     }
}

我曾尝试在 ApplicationPoolsUpdate 操作方法如下,但它不工作:

I have tried the following in the ApplicationPoolsUpdate action method but it doesn't work:

return RedirectToAction("UpdateConfirmation", "ApplicationPool", new { xDocument = updatedResultsDocument });

return RedirectToAction("UpdateConfirmation", new { controller = "ApplicationPool", xDocument = updatedResultsDocument });

我将如何实现这一目标?

How would I achieve this?

推荐答案

HTTP和重定向

让我们回顾一下第一个ASP.NET MVC是如何工作的:

Let's first recap how ASP.NET MVC works:


  1. 当一个HTTP请求时,它是对一组路线的匹配。如果路由的请求匹配,对应于该路由的控制器动作将被调用。

  2. 调用操作方法之前,ASP.NET MVC执行模型绑定。模型绑定是映射的HTTP请求,这基本上只是文本的内容,你的操作方法的强类型参数
  3. 的过程

让我们也提醒自己重定向是什么:

Let's also remind ourselves what a redirect is:

这是HTTP重定向是Web服务器可以发送到客户端,告诉客户端寻找下一个不同的URL请求的内容的响应。新的URL包含在位置头的Web服务器返回给客户端。在ASP.NET MVC中,通过从操作返回 RedirectResult 做一个HTTP重定向。

An HTTP redirect is a response that the webserver can send to the client, telling the client to look for the requested content under a different URL. The new URL is contained in a Location header that the webserver returns to the client. In ASP.NET MVC, you do an HTTP redirect by returning a RedirectResult from an action.

传递数据

如果你只是简单的传递像价值观字符串和/或整数,你可以将它们作为在位置报头中的URL查询参数。这是,如果你使用像会发生什么

If you were just passing simple values like strings and/or integers, you could pass them as query parameters in the URL in the Location header. This is what would happen if you used something like

return RedirectToAction("ActionName", "Controller", new { arg = updatedResultsDocument });

正如其他人建议

这是行不通的原因是,的XDocument 是一个潜在的非常复杂的对象。没有为ASP.NET MVC框架中并没有直接的方法将文档序列化到的东西,将适合从URL值回你的的XDocument 行动的URL,然后绑定模型参数。

The reason that this will not work is that the XDocument is a potentially very complex object. There is no straightforward way for the ASP.NET MVC framework to serialize the document into something that will fit in a URL and then model bind from the URL value back to your XDocument action parameter.

在一般情况下,传递文件给客户,以便客户端传递回服务器上的下一个请求,是一个非常脆弱的过程:它需要系列化和deserialisation的各种和各种事情可能出错。如果文件较大,也可能是带宽的大量浪费,而且可能严重影响应用程序的性能。

In general, passing the document to the client in order for the client to pass it back to the server on the next request, is a very brittle procedure: it would require all sorts of serialisation and deserialisation and all sorts of things could go wrong. If the document is large, it might also be a substantial waste of bandwidth and might severely impact the performance of your application.

相反,你需要做的是保持周围的文件在服务器上,并通过标识符返回给客户端。客户机随后将标识符与下一个请求沿着与服务器检索使用该标识符的文件。

Instead, what you want to do is keep the document around on the server and pass an identifier back to the client. The client then passes the identifier along with the next request and the server retrieves the document using this identifier.

上的下一个请求存储检索数据

所以,现在的问题是,在什么地方存储服务器在此期间的文件?嗯,这是你来决定,最好的选择将取决于您的特定情况。如果该文档需要从长远来看可用,您可能希望将其存储在磁盘上或在数据库中。如果它仅包含瞬时信息,保持它在Web服务器的内存中,在ASP.NET缓存或会话(或的TempData ,这是或多或少相同会话到底)可能是正确的解决方案。无论哪种方式,您存储项下的文件,将允许您检索文档后面:

So, the question now becomes, where does the server store the document in the meantime? Well, that is for you to decide and the best choice will depend upon your particular scenario. If this document needs to be available in the long run, you may want to store it on disk or in a database. If it contains only transient information, keeping it in the webserver's memory, in the ASP.NET cache or the Session (or TempData, which is more or less the same as the Session in the end) may be the right solution. Either way, you store the document under a key that will allow you to retrieve the document later:

int documentId = _myDocumentRepository.Save(updatedResultsDocument);

然后返回键到客户端:

and then you return that key to the client:

return RedirectToAction("UpdateConfirmation", "ApplicationPoolController ", new { id = documentId });

当您要检索文档,您只需基于密钥获取它:

When you want to retrieve the document, you simply fetch it based on the key:

 public ActionResult UpdateConfirmation(int id)
 {
      XDocument doc = _myDocumentRepository.GetById(id);

      ConfirmationModel model = new ConfirmationModel(doc);

      return View(model);
 }

这篇关于不同的控制器操作方法之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆