0

I'm trying to pass a list of URL's with Id attributes from a controller to a view. I can pass a <a href=...> link back but I don't think writing a 'localhost' absolute path is a clean way of approaching this. I cant pass an ActionLink back as it returns the full string. Is ther a simple solution to this problem? Thanks in advance.

4
  • What do you mean "it returns the full string"? An ActionLink should be dynamically rendering the path based on the location of the application, making it portable. (Rather than hard-coding the full path.) How is this not working? Commented Dec 15, 2011 at 19:31
  • Can you post an example of what you need to pass? Commented Dec 15, 2011 at 19:31
  • And please post a little code of what you are doing, what it produces and what you would like to produce. Commented Dec 15, 2011 at 19:32
  • Sorry I thought as I was typing this post that it may have been slightly confusing. I have an accordian menu that is generated in the view by a simple call getMenu() this returns a ul - li structure. The problem is that I don't know how to return url's in the li elements. I'm looking for some kind of url builder? I think? Commented Dec 15, 2011 at 19:34

2 Answers 2

1

Using this overload of the UrlHelper.Action() method and Request object you can get a complete URL including the route parameters such as IDs and the actual hostname of the application.

string url = Url.Action("action", "controller", 
    new System.Web.Routing.RouteValueDictionary(new { id = id }), 
    "http", Request.Url.Host); 

UrlHelper is available in the controller via its Url property.

You can then pass such URL into your view.

It is also possible to use UrlHelper directly inside your view to create URLs for controller actions. Depends if you really need to create them inside the controller.


Edit in response to comments:

Wherever you need to place the URLs, this "URL builder" you are looking for is still the UrlHelper. You just need to pass it (or the generated URLs) where you need it, being it inside the controller, view or custom helper.

To get the links inside the unsorted list HTML structure you mention, you need to put anchors inside the list items like this:

<ul>
    <li><a href="URL">Link</a></li>
    ...
</ul>

Then again you just need to get the URLs from somewhere and that would be from UrlHelper.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry my mistake I'm running this method in the HtmlHelper, you solution doesn't work there but thanks anyway!
@user1084453 I updated the answer according to your comments.
0

Simple and easy.

 <a href="@Url.Action("action","controller",new {id = my_var_id})">text</a>

the route id = the parameter that is going to be inserted into your method.

eg.

function Details(int id) {
   //id has the value of my_var_id
 }

3 Comments

Put that in your view and adjust action and controller according your needs. Eg. If you want to reference /Account/Logon, then Account is your controller and Logon is your action. then it becomes a href="@Url.Action("Logon","Account",new {id = my_var_id})">text</a>
I can't I have several loops running through menus and sub-menus in the controller and using Tag builder to create nested UL elements. Putting all that in the view would defeat the purpose of having a clear seperation of concerns, I like my views to be clean :)
Yeah, me to, but calling @Url.Action and using the advantage of having multiple parameters is the same like calling @ html.actionlink in my point of view. Only it's much more "free" to do as you like. The same thing with images inside links. <a href""><img src="@ url.content("relativepath")</a> their both a "bitch" :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.