3

I'm having a problem trying to use the UrlHelper Action() method to point to the /Home/Index action on my site. I need to generate links to Index dynamically (including an id parameter) in client-side script, so I did the obvious:

var url = '@this.Url.Action("Index", "Home")';
function generateLink ( id ) {
  var anchor = "<a href='" + url + "/" + id + "'>Select Me</a>"
  return anchor;
}

But the anchor tags being generated here look like this:

<a href='http://localhost//1013'>Select Me</a>

which obviously doesn't route to the correct action. I'm assuming that Url.Action() is being "smart" by figuring out that Home and Index are the default values for my default route, so http://localhost and http://localhost/Home and http://localhost/Home/Index are functionally identical, but I need to somehow force it to pick the full URL option.

Is there a way to do this, or am I going to have to build the URL up myself?

EDIT:

I haven't change the routing from the defaults for a new MVC3 project:

public static void RegisterRoutes ( RouteCollection routes )
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        } // Parameter defaults
    );
}

ANSWER:

I finally went with a slight variation of @BFree's answer, just because I prefer Html.ActionLink() to Url.Action if I can use it:

var anchor = '@this.Html.ActionLink("Select Me", "Index", "Home", new { id = "XXX" }, null)';

function generateLink ( id ) {
  return anchor.replace("XXX", id);
}
1
  • We'll need to see the route definitions (all of them, preferably), so we can see if there is a conflict or if there are parameters for the route you are not specifying. Commented May 6, 2011 at 14:07

2 Answers 2

2

One hacky approach is to do something like this:

var url = '@this.Url.Action("Index", "Home", new { id = 1})';
function generateLink ( id ) {
  var anchor = "<a href='" + url.replace("1",id) + "'>Select Me</a>"
  return anchor;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, that works, though I don't like it. If nothing better comes along I'll mark this as the answer. (I don't like it mostly because the URLs look odd: "localhost/?id=1013"
+1 Actually not hacky I don't think. Perhaps new { id = "_placeholder_" } would be better though. Short of having a web service I can't see much other way.
@Michael: Using my approach, it outputs the full url e.g. :/Home/Index/12345
Yah, you're right. After casper's comment I started messing around with my routes and I broke routing :) Your answer is what I went with, mostly.
0

A workaround could be:

function generateLink ( id ) {
  var anchor = "<a href='/?id=" + id + "'>Select Me</a>"
  return anchor;
}

Comments

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.