Html.ActionLink() not working for passing a C# object
Asked Answered
A

1

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).

Absorbed answered 23/8, 2013 at 22:47 Comment(0)
B
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.

Bowles answered 23/8, 2013 at 22:55 Comment(1)
@Absorbed - 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.Loom

© 2022 - 2024 — McMap. All rights reserved.