user576510
user576510

Reputation: 5905

passing model to RedirectToAction without using session or tempdata?

I m modifying an existing code.

From one action I use RedirectToAction to transfer execution control to another action. I need to pass Model with RedirectToAction as well. My idea is it can't be done directly by passing Model to 2nd action without using Session or tempData. But still want to ask is there a technique to pass model with RedirectToAction ? I don't want to put Model in Session or TempData.

Thanks

Upvotes: 0

Views: 500

Answers (2)

AbdulG
AbdulG

Reputation: 755

I would expect the code of the previous answer to throw an exception when attempting to implicitly convert an object of parameter1 and parameter2 to ModelClass.

That being said, The best way is to just pass the id of the entity to your new action, then access your repository to initialize your model with the id that has been passed. Lets assume you have initialized a user with a UserID property.

return RedirectToAction("NextAction", new { id = user.UserID });

Then in NextAction just initialize the model with the passed id

Upvotes: 0

TlonXP
TlonXP

Reputation: 3485

You can try something like that, but it doesn't feel like a natural action:

 public ActionResult Index()
 {
     return RedirectToAction("AnotherAction", new
     {
         Parameter1 = Parameter1,
         Parameter2 = Parameter2,

      });
 }

 [HttpGet]
 public ActionResult AnotherAction(ModelClass model)
 {
     //model.Parameter1
     //model.Parameter2
     return View(model);
 }

Upvotes: 1

Related Questions