-1

I want to pass model member values to this controller from RedirectToAction

public ActionResult UserRegistraion1(UserProfilePL objpl)

Using TempData model to pass on this RedirectToAction i don't want to make individual TempData to insert every model member on it single TempData model to retrieve at this UserProfilePL how can i achieve this.

Model passing to this Action below.

UserProfilePL objpl = new UserProfilePL();
ViewBag.msg = objpl.Code;
                objpl.LoginID = emailID;
                objpl.FBGMid = ID;
                objpl.type = type;
                objpl.Name = name;
                objpl.ImageUrl = ImgUrl1;

                //TempData[""] = 

                return RedirectToAction("UserRegistraion1", "Home");

I want to pass objpl model into this RedirectToAction without individual TempData.

  • You do not need _individual TempData to insert every model member_. you can just add the model to `TempData`. But `TempData` only lasts one request, so it means that if the user refreshes the page, the data will be lost. Pass the `ID` of the model to the `UserRegistraion` method and get the data object from the database again. –  Mar 21 '17 at 08:32
  • Include your UserRegistration View code with question. – Ajinder Singh Mar 21 '17 at 08:56
  • Sir Stephen Muecke this won't work as ID is created within the database and email ID is fetched from Facebook and Gmail which is not possible for me to get data from database which i would say don't exist as it is for new registration – Rinki Panwar Mar 21 '17 at 09:23
  • Sir Ajinder Singh i have updated the code as you can see what i want from it – Rinki Panwar Mar 21 '17 at 09:27

1 Answers1

0

Do like this in your Action method. See I'm keeping the complete Model in Tempdata

TempData["myObject"] = objpl;

Then inside your UserRegistraion Action method,

if (TempData["myObject"] != null)
{
    UserProfilePL profile = (UserProfilePL) TempData["myObject"];
    //rest code
}

To keep single value in TempData,

TempData["myEmail"] = emailID;

Then you can retrive the value in UserRegistraion Action method.

Basanta Matia
  • 1,504
  • 3
  • 15
  • 27
  • How do i pass it on return RedirectToAction("UserRegistraion1", "Home"); with TempData model – Rinki Panwar Mar 21 '17 at 12:14
  • There is no need to pass this TempData. You just need to write this before this line of your Method. RedirectToAction("UserRegistraion1", "Home"); – Basanta Matia Mar 21 '17 at 12:16
  • Then u can automatically fetch the data in UserRegistraion1 Method. I have already wrote the code how to extract data, please check once my answer. – Basanta Matia Mar 21 '17 at 12:17
  • i understood completely, my concern is that i want to add model TempData value and pass it on this public ActionResult UserRegistraion(UserProfilePL objpl) on this method – Rinki Panwar Mar 21 '17 at 12:22
  • RedirectToAction("UserRegistraion", "Home" , new { TempData["myObject"]}); like this method – Rinki Panwar Mar 21 '17 at 12:24
  • No. U can't pass like this. Then what is use of TempData ?? – Basanta Matia Mar 21 '17 at 12:26
  • Actually i don't want to create a new method to pass TempData i just want to pass model on the RedirectToAction is it possible – Rinki Panwar Mar 21 '17 at 12:29
  • In that case u can return a view with the model data. Ex: return view("UserRegistraion1",objpl); – Basanta Matia Mar 21 '17 at 12:34
  • What is ur issue with the TempData ? U can't pass a complex object in RedirectToAction(). Check this http://stackoverflow.com/questions/22505674/can-we-pass-model-as-a-parameter-in-redirecttoaction – Basanta Matia Mar 21 '17 at 12:45
  • i found a solution though return View("~/Views/Home/UserRegistraion1",objpl) how do i get the actual path – Rinki Panwar Mar 21 '17 at 12:49
  • That is what I already mentioned here. return view("UserRegistraion1",objpl); This is what u r doing now. – Basanta Matia Mar 21 '17 at 12:52
  • Yes but error is throwing can't find the path issue – Rinki Panwar Mar 21 '17 at 12:56
  • The view 'UserRegistraion1' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/UserRegistraion1.aspx ~/Views/Home/UserRegistraion1.ascx ~/Views/Shared/UserRegistraion1.aspx ~/Views/Shared/UserRegistraion1.ascx ~/Views/Home/UserRegistraion1.cshtml ~/Views/Home/UserRegistraion1.vbhtml ~/Views/Shared/UserRegistraion1.cshtml ~/Views/Shared/UserRegistraion1.vbhtml – Rinki Panwar Mar 21 '17 at 12:56
  • Do u have a UserRegistraion1 View or not ?? – Basanta Matia Mar 21 '17 at 12:59
  • yes of course i have a view with model data as a parameter passing on it – Rinki Panwar Mar 21 '17 at 13:00
  • See, U need to pass by TempData as I mentioned in my answer. Return View("UserRegistraion1",objpl) will only load the view, it wont call the Action Method. U can't pass complex object in RedirectToAction(); – Basanta Matia Mar 21 '17 at 13:04
  • Yeah last time i tried to pass model on redirect but didn't work MVC do have its limitations thank you for your time sir – Rinki Panwar Mar 21 '17 at 13:05
  • You just need to click on mark as answer in my solution and click on upvote button :) Thanks – Basanta Matia Apr 15 '17 at 17:06