Chiefy
Chiefy

Reputation: 179

MVC Url relative to variable ActionResult

Reasonably new to MVC. My problem is that I have a controller that has 3 different ActionResult's which are called depending on an enum.
All three action results return the same view but with different lists as the views model. In the view the user should be able to click on an item in the list and view the details based on the ID of the item.
e.g. Site/Facilities/Libraries returns the list of libraries, Site/Facilities/Libraries/1 returns the details. This works fine when you enter the full path but on the View itself clicking the anchor

<a href="@item.ID">@item.Name</a>

on an item in the list returns the Url Site/Facilities/1 instead of Site/Facilities/Libraries/1. I cant use an ActionLink as the Action to call is dynamic. I know this could be solved by creating a different View for each type but I wondered if there might be another way?

Thanks in advance.

Upvotes: 0

Views: 583

Answers (2)

Egor4eg
Egor4eg

Reputation: 2708

You can use such code (a javascript trick):

<a href="" onclick="window.location = window.location + @item.ID">@item.Name</a>

Or, if you want to use href property, you can use such code:

<%=Html.ActionLink(item.Name, "Facilities", ViewData("ActionName"), new {id = item.ID}) %>

(In that case you have to specify ViewData("ActionName") in controller).

Upvotes: 1

Robin
Robin

Reputation: 575

I would suggest that you change it so that you only have one Action, but it takes an argument instead, and depending on the argument you push different lists to the View (perhaps using 3 different "helper-functions" instead). That's at least how I would implement what you are describing!

Good luck!

Upvotes: 1

Related Questions