views:

8

answers:

1

I'm new to MVC2 and was wondering if it was possible to pass an object into a Controller Action from a View?

e.g. I've created a ViewModel containing a list of DirectoryInfo objects and pass this to my Index view.

In the view, I loop through all of the DI objects and for each one create an ActionLink.

<%:Html.ActionLink(linkText: subfolder.Name,actionName: "Reports",
                routeValues: new {folder=subfolder}, htmlAttributes:null )%>

but in my Reports action, the "folder" is always null?

 public ActionResult Reports(DirectoryInfo folder)
    {
      //folder is always null here

Is this type of thing possible, or does the routingValue always have to be a primitive?

(ps. I have searched StackOverflow and t'internet and although I can find people asking the same question, I can't find a solution

A: 

DirectoryInfo is a complex object and I don't think you will be able to put it directly into HTML. You need to send something simple like path of directory or file from which controller can construct the required DirectoryInfo again.

TheVillageIdiot
Thanks - I'd started off that way with the Path, so I'll carry on. Just one of those things you think about!
BlueChippy