Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass data from Action To Another Action

How would you pass the model from an (GetDate) action to another (ProcessP) action via RedirectAction method?

Here's the source code:

[HttpPost]
public ActionResult GetDate(FormCollection values, DateParameter newDateParameter)
{
    if (ModelState.IsValid)
    {
return RedirectToAction("ProcessP");
    }
    else
    {
return View(newDateParameter);
    }
}


public ActionResult ProcessP()
{
   //Access the model from GetDate here??
    var model = (from p in _db.blah
 orderby p.CreateDate descending
 select p).Take(10);

    return View(model);
}
like image 384
scv Avatar asked Jun 09 '12 00:06

scv


People also ask

How you can send a data from action method to another action method?

How to pass data from one action method to another action method? Sometimes, we may need to pass some data from one action method to another while redirecting the user to another action method. In this case, we can pass certain data through the RedirectToAction method by objectParamter.

How do you pass data between action methods in MVC?

There are a number of ways in which you can pass parameters to action methods in ASP.NET Core MVC. You can pass them via a URL, a query string, a request header, a request body, or even a form. This article talks about all of these ways, and illustrates them with code examples.

How do I move an object from one action method to another?

You can use TempData to pass the object. This worked well, Thanks Eranga. One potential pitfall is that the object can be disposed before it gets used in the view if you have a overridden definition for Dispose in your controller(something VS tends to add in with its auto generated CRUD code).

How you can send a data from action method to view?

ViewBag is a very well known way to pass the data from Controller to View & even View to View. ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary. Let's see how it is used.


1 Answers

If you need to pass data from one action to another one option is to utilize TempData. For example within GetDate you could add data to the session as follows:

TempData["Key"] = YourData

And then perform the redirect. Within ProcessP you can access the data utilizing the key you previously used:

var whatever = TempData["Key"];

For a decent read, I would recommend reading through this thread: ASP.NET MVC - TempData - Good or bad practice

like image 112
Jesse Avatar answered Sep 18 '22 11:09

Jesse