0

Im new to MVC. Im fooling around trying to grasp the concept. I have the following code. Its quite straight forward. When the user hits the sort action, it sorts a list and sends the list to the Index Action (there are many other ways how to achieve this, like for example sending a sort bool - As I said, Im just fooling around).

The problem Im encountering is that the model parameter in the Index action is always null. While running (debugging) code is going through the Sort Action and the m=model is not null (has a list of users). I can also follow that it goes directly to index. Can someone please tell me what Im doing wrong? Help will be much appreciated.

public ActionResult Index(List<user> model)
        {
            if (model == null)
            {
                model = (from u in UsersList
                             select u).ToList<user>();
            }

            return View(model);
        }

 public ActionResult Sort()
        { 
            var model = from f in UsersList
                      orderby f.Name ascending
                      select f;

            return RedirectToAction("Index", new {m = model});

        }
zed
  • 2,298
  • 4
  • 27
  • 44
user1203996
  • 89
  • 1
  • 11

1 Answers1

1

What you are doing here is passing model as parameter

RedirectToAction("Index", new {m = model});

but it's not allowed. You have to pass the model directly

RedirectToAction("Index", model);

If you still want to pass it as a parameter take a look on this post

Community
  • 1
  • 1
AntonS
  • 341
  • 1
  • 8
  • Ok. Thank you. However this triggers another question. How do you get access to the passed on model? – user1203996 Aug 07 '15 at 07:44
  • @ user1203996, I didn't get your comment. In which case don't you understand how to access model? – AntonS Aug 10 '15 at 18:38
  • Why do you pass a model directly? It isn't allow with MVC using RedirectToAction. TempData should be the case. – Rozen Jun 07 '18 at 13:37