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",
"{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
:
, , @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);
}