0

I have a website in ASP.NET MVC3. I want to pass collection to another view. I have this collection in ViewBag. Here is my ActionLink:

@Html.ActionLink("Show Report", "Report", new { workList = ViewBag.workReportList }) 

Controller:

public ActionResult Report(List<Work> workList)
{
    return View(workList);
}

But it is not working - it looks like the passed object is empty. When I am using the same object to RenderPartial everything is working fine.

@{Html.RenderPartial("WorkListTable", (IEnumerable<WorkWeb.Entities.Work>)ViewBag.workReportList);}

How can I pass this object to a View? Any help much appreciated!

Marta
  • 2,857
  • 13
  • 46
  • 67

2 Answers2

0

Did you try this (passing the controller name)?

@Html.ActionLink("Show Report", "Report", "ControllerName", new { workList = ViewBag.workReportList }, null)
Leon Cullens
  • 12,276
  • 10
  • 51
  • 85
0

I've had this problem before, it's to do with routing. change workList to id in both your ActionLink and in your ActionResult or add a new route where you accept workList as a parameter, something like:

routes.MapRoute(
            "ReportRoute", // Route name
            "{controller}/{action}/{workList}", // URL with parameters
            new { controller = "ControllerName", action = "Report", workList = UrlParameter.Optional }
        );

Replacing ControllerName with the name of your controller

CallumVass
  • 11,288
  • 26
  • 84
  • 154
  • Oh, my bad, I remember you can't post a collection via a link like that, you could do something suggested here perhaps? http://stackoverflow.com/questions/5682944/how-to-pass-ienumerable-model-in-parameter-of-html-actionlink – CallumVass May 17 '12 at 16:53