ASP.NET MVC UrlHelper.Action () does not return the correct path

I am having a problem using a method UrlHelper Action()to indicate an action /Home/Indexon my site. I need to generate links to Indexdynamically (including parameter id) 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 created here look like this:

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

which, obviously, does not direct to the correct action. I assume that it Url.Action()’s “smart”, figuring out that Homeboth Indexare the default values ​​for my route by default, http://localhostand therefore http://localhost/Homethey are http://localhost/Home/Indexfunctionally identical, but I need to somehow make it select the full URL.

, URL-?

EDIT:

MVC3:

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
    );
}

:

, , @BFree, Html.ActionLink() Url.Action, :

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

function generateLink ( id ) {
  return anchor.replace("XXX", id);
}
+3
2

- - :

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;
}
+2

:

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

All Articles