Reputation: 8405
I am using RedirectToAction to load a new page after login and pass in the database model with it.
RedirectToAction("AdministrationPortal", "Manage", person);
person.user model is a User class created by the entity framework. The model of person is
class Person {
public User user { get; set; };
public string roleType { get; set; };
}
I node the person object did hold the data at the time where RedirectToAction got called.
Don't know why when I user it on a portal page @Model.Person.user
is null.
@model namespace.Model.Person
Upvotes: 3
Views: 2150
Reputation: 2684
When I need to send a model in RedirectToAction
, I simply pass id like (lets say you person has id):
return RedirectToAction("AdministrationPortal", "Manage", new { @id=person.Id} );
Then at controller:
public ActionResult AdministrationPortal(long id)
{
//Get Person from DB
var model=GetPersonById(id);
return View(model);
}
I personally don't like using TempData but if you like to use it then:
TempData["Person"] =person;
return RedirectToAction("AdministrationPortal", "Manage");
Upvotes: 1
Reputation: 17194
You cannot pass complex objects when redirecting using RedirectToAction
.
It is used for passing the routeValues
not the Model.
To maintain state temporarily for a redirect result, you need to store your data in TempData
.
You can refer this for more info: passing model and parameter with RedirectToAction
TempData["_person"] = person;
return RedirectToAction("AdministrationPortal", "Manage");
Then
public ActionResult AdministrationPortal()
{
Person p_model = (Person)TempData["_person"];
return View(p_model);
}
Upvotes: 5