2

I have the following Custom URL Routing Rule:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
          "RaceRoute", // Route name
          "people/create/{raceid}/{id}", // URL with parameters
          new { controller = "People", action = "Create", raceid = UrlParameter.Optional, id = UrlParameter.Optional }
          );

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

Which I'm trying to use with Actionlink

@Html.ActionLink("Add Person", "Create", "People", new { raceid = item.RaceId, id="1" })

I basically want the url to look like "/people/create/5/1"

But the HTML generated looks like

<a href="/races/Create?Length=6" id="1" raceid="5">Add Person</a>

It should say <a href="/people/Create/5/1">Add Person</a>

The page I'm on is http://localhost:57355/races

If I do just @Html.ActionLink("Add Person", "Create", "People") then it works but I get no parameters.

What am I missing?

Thanks

1 Answer 1

2

I think you want this overload of the method. Add a null at the end:

@Html.ActionLink("Add Person", "Create", "People", new { raceid = item.RaceId, id="1" }, null)

Here is the overload:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    RouteValueDictionary routeValues,
    IDictionary<string, Object> htmlAttributes
)

or is it this one:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
)
Sign up to request clarification or add additional context in comments.

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.