0

I have a 2 views with model as Account. From view one, I am using RedirectToAction to go to view two and sending the model object as below:

    [HttpPost]
    public ActionResult Login(Account account)
    {
           //Some code here
            return RedirectToAction("Index", "AccountDetail", account);
    }

AccountDetail controller looks like this:

 public ActionResult Index(Account account)
    {
        return View("ViewNameHere", account);
    }

The model object contains a property like this:

 public class Account
 {
 // Some code here
 public List<Details> Details{
 get;
 set;
 }

In the first controller, before making call to RedirectToAction there is one item in Details. However, in the Index method of second controller, there is nothing.

Can someone help pointing out the flaw here? Since I am beginner with MVC, cannot seem to figure it out.

1 Answers1

2

You should not pass a complex object to a GET method. Apart from the ugly URL it would create, you could easily exceed the query string limit and throw an exception.

In any case you cannot pass a collection (or a complex object containing a collection) to a GET method using RedirectToAction(). Internally the method uses reflection to generate the query string by calling the .ToString() method of each property of the model, which in the case of your collection property will be something like ../AccountDetail/Index?Details=System.Collections.Generic.List<Details>.

When the Index() method is called, a new instance of Account is initialized and the value of its property Details is attempted to be set to the string System.Collections.Generic.List<Details> which fails and the result is that property Details is null.

Options include passing an identifier and get the collection from the repository or Session or TempData