0

I'm trying to pass down parameters to my Action. There are several parameters that I need to pass down. When debugging, I found that the simple types of parameters got their values, whereas my own class parameters is null.

return RedirectToAction("Histories", new {MyUser = user, sortOrder = "name_desc" }); 

And here is the Action method:

   public ActionResult Histories(ApplicationUser MyUser, string sortOrder, int? page)

I did a research and found , it seems that only objects which can be serialized can be passed down.

So I simply added an annotation [Serializable] on my ApplicationUser class, and it doesn't work.

So I'm wondering what's the best practice to pass down my objects?

I certainly know I can put the MyUser into Session["CurrentUser"], but I just don't like this old fashion.

Thank you.

Franva
  • 6,565
  • 23
  • 79
  • 144
  • `TempData["CurrentUser"]` is the most common way, but you can also look into a more complex setup with Server.TransferRequest – arserbin3 May 29 '14 at 17:17
  • possible duplicate: http://stackoverflow.com/questions/5409899/pass-complex-object-with-redirect-in-asp-net-mvc – Oleksii Aza May 29 '14 at 17:34
  • hi @OleksiiAza that post uses the same thing TempData[] to pass objects – Franva May 30 '14 at 00:36

1 Answers1

0

You have not passed int? page value, it should be like this

return RedirectToAction("Histories", new {MyUser = user, 
           sortOrder = "name_desc", 
           page =1 });

or you need to use default parameter value like this

public ActionResult Histories(ApplicationUser MyUser,
     string sortOrder, 
     int? page = 1)
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85