Passing collections as parameters from view to controller and via RedirectToAction

Last post 04-24-2009 2:58 PM by levib. 3 replies.

Sort Posts:

  • Passing collections as parameters from view to controller and via RedirectToAction

    04-23-2009, 3:55 PM
    • Member
      1 point Member
    • SergID
    • Member since 11-18-2008, 7:23 PM
    • Posts 2

    I'm implementing the following scenario:

    There is view with a catalogue of items (like products) and a filter for them. Filter is a  list of checkboxes and each of the checkboxes represents one category for an item. The number of categories is not fixed.

    In my controller I get all the date for the filter with the following method:

    [AcceptVerbs(HttpVerbs.Post)]
            public RedirectToRouteResult List(int? page, FormCollection formCollection)
            {
                IEnumerable<int> categoryFilter = selectFilterIDs(formCollection, "category");    //parsing the Form Collection object

                return RedirectToAction("List",
                    new
                    {
                        page = 1,
                        filterCategoryIDs = categoryFilter
                    });
            }

    this method passes all the data to:

    [AcceptVerbs(HttpVerbs.Get)]
            public ActionResult List(int? page, IEnumerable<int> filterCategoryIDs)
            {

                //restore selected and unselected values
                ViewData["selectedCategories"] = filterCategoryIDs;

      .............


            }

    So after the redirect I get an empty IEnumerable<int> filterCategoryIDs. It is not equal to null, it is just empty.

    The same happens when I try to pass data from my view when making the pagination:

    <%= Html.Pager(

    ViewData.Model.PageSize,

    ViewData.Model.PageNumber,

    ViewData.Model.TotalItemCount,

    new { filterOrderTypeIDs = ViewData["selectedOrderTypes"], filterCategoryIDs = ViewData["selectedCategories"] })

    %>

     

    So the question is how to pass collections as parameters properly

  • Re: Passing collections as parameters from view to controller and via RedirectToAction

    04-23-2009, 4:53 PM
    Answer
    • Contributor
      6,387 point Contributor
    • Augi
    • Member since 10-18-2008, 9:46 AM
    • Czech Republic
    • Posts 1,102

    You cannot pass parameter in this way. Check this my article on data passing when redirecting. The best way could be using TempData dictionary - just store your collection into TempData before redirect and read this value from TempData after redirecting.

    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: Passing collections as parameters from view to controller and via RedirectToAction

    04-24-2009, 12:30 PM
    • Member
      1 point Member
    • SergID
    • Member since 11-18-2008, 7:23 PM
    • Posts 2

    Everything works good with TempData

    Maybe that's a stupid question but why can't I pass collections as parameters for action methods?

  • Re: Passing collections as parameters from view to controller and via RedirectToAction

    04-24-2009, 2:58 PM
    • Contributor
      4,601 point Contributor
    • levib
    • Member since 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 792
    • AspNetTeam

    You can pass collections as parameters for action methods.  See http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx for all the gory details.

    However, parameters are read from the request (like the querystring + form), not from TempData.  So if you're submitting data from the form to a controller, the parameters will be auto-populated.  But if you're redirecting from one action to another, that data has to be round-tripped through TempData, and we don't auto-bind from that.

Page 1 of 1 (4 items)