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).
HTML Links With MVC, the Html. ActionLink() does not link to a view. It creates a link to a controller action. The first parameter is the link text, and the second parameter is the name of the controller action.
ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor. Hence in order to submit (post) Form using @Html. ActionLink, a jQuery Click event handler is assigned and when the @Html.
Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With