6

I'm trying to pass an object through an ActionLink to a method in an MVC controller.

The razor syntax:

@Html.ActionLink("Export to Excel","ReturnExcelOfViewableResponses",new { SearchObject = Model.SearchObject})

What's actually being displayed in markup:

<a href="/EducationAgency/ReturnExcelOfViewableResponses?SearchObject=DTO.SearchObject" tabindex="29">Export to Excel</a>

The controller method is being called just fine, hence no reason to post here. What needs to be done so that the actual values are passed into the actionLink instead of DTO.SearchObject? According to HTML.ActionLink method it looks like I have the right syntax (using MVC 4).

Community
  • 1
  • 1
levininja
  • 3,118
  • 5
  • 26
  • 41

1 Answers1

9

You should be able to pass the DTO, assuming it's just that, as the parameter into ActionLink:

@Html.ActionLink("Export to Excel","ReturnExcelOfViewableResponses",Model.SearchObject)

Any public fields will be added as a query parameter key/value pair.

Justin Bicknell
  • 4,804
  • 18
  • 26
  • 1
    @levininja - nah, contrary to what the answer you linked to in your question says, I'm quite sure there have been no major differences between MVC 1, 2, 3 and 4 for these methods. This functionality is just rarely used compared to passing an anonymous object (like the example in your question). In either case, the passed object defines route values - and the (first level) properties of the object are (eventually) turned into query parameters by reflection - doesn't matter what object it is. But the anonymous object you pass in your question has only one "first level" property: SearchObject. – JimmiTh Aug 23 '13 at 23:42