Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 726
levininja Avatar asked Aug 23 '13 22:08

levininja


People also ask

How do I use ActionLink in HTML?

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.

How do I post on ActionLink?

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.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


1 Answers

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.

like image 81
Justin Bicknell Avatar answered Sep 22 '22 13:09

Justin Bicknell