Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass complex object with redirect in ASP.NET MVC?

Hi,

I have a action that looks like this :

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register(AdRegister adRegister, IEnumerable<HttpPostedFileBase> files)

The AdRegister is a complex class and I need to pass this in to a redirect method further down in the Register action, like this :

return this.RedirectToAction("Validate", adRegister);

The Validate action looks like this :

public ActionResult Validate(AdRegister adRegister)

I do know that I can pass simple parameters but in this case it´s a complex object. This example do not work, the adRegister´s properties will be null.

Is this posible and if so, how?

BestRegards

More Information : Register action will take the adRegister and do som magic on it, then It will be sent to the Validate action. The Validate action will return a validation page to the user. When the user hit the grant button the adRgister will be filled from the form and then sent to the vValidate post where it will be saved. I have looked in to place the adRegister in cache or database temporarily but it will be better if I could simple pass it to the next action.

like image 358
Banshee Avatar asked Mar 23 '11 18:03

Banshee


People also ask

How to create redirect action result in ASP NET Core MVC?

Redirect Action Result in ASP.NET Core MVC. Step 1. Open Visual Studio 2019 and select the ASP.NET Core Web Application template and click Next. Step 2. Name the project FileResultActionsCoreMvc_Demo and click Create. Step 3. Select Web Application (Model-View-Controller), and then select Create. ...

What is redirecttoaction method in MVC?

RedirectToAction Result in MVC The RedirectToAction Result is returning the result to a specified controller and action method. Controller name is optional in RedirectToAction method. If not mentioned, Controller name redirects to a mentioned action method in the current Controller.

What is redirecttorouteresult in ASP NET MVC?

It is rendered to the page by URL. If we give the wrong URL, it will show 404-page errors. The RedirectToRouteResult is used whenever we need to go from one action method to another action method within the same or different controller in ASP.NET MVC Application.

What is ASP NET MVC?

ASP.NET MVC is an open source and lightweight web application development framework from Microsoft. This book has been written to prepare yourself for ASP.NET MVC Interview. This book is equally helpful to sharpen their programming skills and understanding ASP.NET MVC in a short time.


3 Answers

One possibility would be to pass the simple properties in the query string:

return RedirectToAction(
    "Validate", 
    new { 
        foo = adRegister.Foo, 
        bar = adRegister.Bar, 
        ... and so on for all the properties you want to send
    }
);

Another possibility is to store it in TempData (for the lifetime of the redirect) or Session (for the lifetime of the ASP.NET session):

TempData["adRegister"] = adRegister;
return RedirectToAction("Validate");

and then retrieve it from TempData:

public ActionResult Validate()
{
    adRegister = TempData["adRegister"] as AdRegister;
    ...
}

Yet another possibility (and the one I would recommend you) is to persist this object in the POST method in your datastore:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(AdRegister adRegister, IEnumerable<HttpPostedFileBase> files)
{
    ...
    string id = Repository.Save(adRegister);
    return RedirectToAction("Validate", new { id = adRegister.Id });
}

and then fetch it from the data store after you redirect:

public ActionResult Validate(string id)
{
    AdRegister adRegister = Repository.Get(id);
    ...
}
like image 82
Darin Dimitrov Avatar answered Oct 24 '22 07:10

Darin Dimitrov


an idea would probably create a session variable and pass around a Key that references that session variable if the object is required acorss a few views?

like image 45
Mark Redman Avatar answered Oct 24 '22 07:10

Mark Redman


ASP.NET MVC's tempdata should be perfect for this.

That said, TempData or Session is one option, but has some downsides like being quite violate and oftentimes murky or difficult to debug. What might be preferable is to "stash" the temporary value in a persistent store, such as the user's profile or your own database, then pass a key through the validate method which can then load the data from said store. This also opens up the possibility of recovering abandoned carts and such.

like image 32
Wyatt Barnett Avatar answered Oct 24 '22 08:10

Wyatt Barnett