Transferring data between different controller action methods

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

Here is my source action method where I want to pass 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(); } } 

I tried the following in the ApplicationPoolsUpdate action method, but it does not work:

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

How can i achieve this?

+64
c # asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
Mar 13 '13 at 12:31 on
source share
5 answers

HTTP and redirects

Let's first look at how ASP.NET MVC works:

  • When an HTTP request arrives, it maps to a set of routes. If the route matches the request, the controller action corresponding to the route will be activated.
  • Before calling the action method, ASP.NET MVC binds the model. Model binding is the process of matching the contents of an HTTP request, which is basically text, to the strongly typed arguments of your action method.

Let's also remind ourselves what a redirect is:

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

Data transfer

If you simply passed simple values, such as strings and / or integers, you could pass them as query parameters in the URL in the Location header. This is what will happen if you used something like

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

as suggested by others

The reason this will not work is because XDocument is a potentially very complex object. There is no easy way for an ASP.NET MVC framework to serialize a document into something that will fit into the URL, and then bind the model to the value of the URL back to the XDocument action XDocument .

In general, sending a document to the client so that the client sends it back to the server on the next request is a very fragile procedure: this will require all kinds of serialization and deserialization, and all kinds of things may go wrong. If the document is large, it can also become a significant loss of bandwidth and can greatly affect the performance of your application.

Instead, you want to save the document on the server and pass the identifier to the client. The client then passes the identifier along with the next request, and the server retrieves the document using that identifier.

Saving data for search by next query

So now the question is, where does the server store the document in the meantime? Well, it's up to you, and the best choice will depend on your specific scenario. If this document should ultimately be available, you may want to save it to disk or to a database. If it contains only temporary information that is stored in the memory of the web server in the ASP.NET or Session cache (or TempData , which more or less matches the Session at the end), there may be a correct solution. In any case, you store the document under the key, which will allow you to receive the document later:

 int documentId = _myDocumentRepository.Save(updatedResultsDocument); 

and then you return this key to the client:

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

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

  public ActionResult UpdateConfirmation(int id) { XDocument doc = _myDocumentRepository.GetById(id); ConfirmationModel model = new ConfirmationModel(doc); return View(model); } 
+55
Aug 08 '14 at 10:42 on
source share

Have you tried using ASP.NET MVC TempData ?

The ASP.NET MVC TempData dictionary is used to exchange data between controller actions. The TempData value is saved until it is read, or until the current user session ends. Persistent data in TempData is useful in scenarios such as redirection when values ​​are needed in a single query.

The code would be something like this:

 [HttpPost] public ActionResult ApplicationPoolsUpdate(ServiceViewModel viewModel) { XDocument updatedResultsDocument = myService.UpdateApplicationPools(); TempData["doc"] = updatedResultsDocument; return RedirectToAction("UpdateConfirmation"); } 

And in ApplicationPoolController:

 public ActionResult UpdateConfirmation() { if (TempData["doc"] != null) { XDocument updatedResultsDocument = (XDocument) TempData["doc"]; ... return View(); } } 
+55
Mar 13 '13 at 15:08
source share

Personally, I do not like to use TempData, but I prefer to transfer a strongly typed object, as described in Transferring Information Between Controllers to ASP.Net -MVC .

You should always find a way to make it explicit and expected.

+11
Mar 13 '13 at 12:53 on
source share

I prefer to use this instead of TempData

 public class Home1Controller : Controller { [HttpPost] public ActionResult CheckBox(string date) { return RedirectToAction("ActionName", "Home2", new { Date =date }); } } 

and other controller Action

 public class Home2Controller : Controller { [HttpPost] Public ActionResult ActionName(string Date) { // do whatever with Date return View(); } } 

it's too late, but I hope to be of service to anyone in the future

+2
Nov 03 '16 at
source share

If you need to transfer data from one controller to another, you must transfer data by route values. Because both are different requests. If you are sending data from one page to another, you must have a user query string (the same as route values).

But you can do one trick:

In your call, the called action is called as a simple method:

 public class ServerController : Controller { [HttpPost] public ActionResult ApplicationPoolsUpdate(ServiceViewModel viewModel) { XDocument updatedResultsDocument = myService.UpdateApplicationPools(); ApplicationPoolController pool=new ApplicationPoolController(); //make an object of ApplicationPoolController class. return pool.UpdateConfirmation(updatedResultsDocument); // call the ActionMethod you want as a simple method and pass the model as an argument. // Redirect to ApplicationPool controller and pass // updatedResultsDocument to be used in UpdateConfirmation action method } } 
-3
Mar 13 '13 at 13:09
source share



All Articles