asp.net cleanest way to pass object to ActionResult

235 views Asked by At

Currently, our application has a lot of ActionLinks that only pass an id to the controller, where the object is retrieved again (that was already present/known on the previous page). Is there any cleaner way to do this, like pass the object instead? As our backend isn't too quick it takes a few seconds to load in the popup which you get after you click on the ActionLink.

I've seen some solutions using ajax to post the object but that doesn't seem like the neatest solution to paste those js lines under/at every ActionLink that is used in combination with a popup.

1

There are 1 answers

0
Purvesh Sangani On

The @Html.ActionLink() method will generate a url link to the given Controller/Action. Thus, it can only contain parameters that can be contained in the url of the link. So you cannot pass an object through on the url.

If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).

Parameters in the ActionLink are set through the collection that you passed in as the third item in your function call above. Assuming default routing, this would give an address that looks like /Admin/EditPage/?name=XXX where XXX is the value of menuitem.Title. If you included something else here like itemId = menuitem.Id then it would add this as a query string parameter to the url generated, which would then be accessible to the action that is the target of this link.