2

I'm having some trouble grasping MVC routing. I've read and read and it still doesn't make much sense. I'm trying to map the route "Teams/Summary?teamID=30" so that it displays "Teams/30" in the browser. This is what I have at the moment but I can't understand why it doesn't work:

//Teams routes
routes.MapRoute(
        "TeamsSummary",
        "Teams/{teamID}",
        new { controller = "Teams", action = "Index", teamID = UrlParameter.Optional }
    );

By "doesn't work", I mean that the webpage loads but it still displays "Teams?teamID=30" in the browser address bar.

Could someone help me to understand what I need to do to perform such a simple mapping? By the way, I don't have any other routes set up that I think would interfere and it is listed before the default route in my Global.asax.cs file.

If you have some good articles and tutorials for this subject, that would be helpful as well. Thanks.

Edit: The route works fine if I type the URL in manually. The problem now occurs when I use a form to post back to the controller:

Controller:

public ActionResult Index(TeamsViewModel model)
    {
        if (model.teamID != null)
            model.TeamSummary = new TeamSummaryViewModel(model.teamID);

        return View(model);
    }

View:

@{
ViewBag.Title = "Teams";
}

@using (Html.BeginForm("Index","Teams",FormMethod.Get))
{
    @Html.DropDownList("teamID", new SelectList(Model.Teams,"teamID","Name"))    
    <input type="submit" value="Filter"/>
}
@if (Model.TeamSummary != null)
{
    Html.RenderPartial("_TeamSummary", Model.TeamSummary);
}

Edit2: Changed the original action to the Index on the TeamsController rather than Summary as it was previously.

2
  • 2
    Can you show the Html.ActionLink code that you're using to generate the link? Does the route work if you manually type in one that should work? Commented Nov 23, 2012 at 21:22
  • Hi, thanks. The route works fine when I type it into the browser manually. Edited my question to include the controller and view. Commented Nov 24, 2012 at 19:08

5 Answers 5

1

What happens is that there's another route defined before the TeamsSummary route that is matching, you need to change the order of the routes or add constraints to make sure you match the one you want. Routing can be tricky, if you want to learn more I recommend reading this post.

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

Comments

1

You routing works perfectly well:

Global.asax

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

            routes.MapRoute(
                "TeamsSummary",
                "Teams/{teamID}",
                new { controller = "Teams", action = "Summary" }
            );

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

Link created in html View:

<%= Html.ActionLink("Summary", "Summary", "Teams", new {teamID = 11 },null)%>

Comments

1

Please check proper route name, must match same as specified in Global.asax file while creating an ActionLink.

Comments

1

At first glance, the route doesn't appear to have anything obviously wrong with it.

Maybe you need to specify a default for the TeamID? Wouldn't hurt to add a restriction to the TeamID parameter as to better define the route and allow other routes under Teams

E.g.

context.MapRoute(
        "TeamsSummary",
        "Teams/{teamID}",
        new { controller = "Teams", action = "Summary", teamID = "" },
        new { teamID = @"\d+" }
    );

UPDATE

Your code

Html.BeginForm("Index","Teams",FormMethod.Get)

Is going to create a form which sends information back to the browser as a GET rather than a POST. Which basically means that all the input values will be attached as querystring values... this is done client side and has nothing to do with ASP.NET MVC Routing.

Maybe checkout the W3 Schools article on form action if you are unfamiliar - http://www.w3schools.com/tags/att_form_action.asp.

Comments

0

You can take away the traditional routing if you want, as suggested by this fellow here

most likely if you follow that guide this will is where you could came up :

[Route("Teams/{teamID}")]    

public ActionResult Index(TeamsViewModel model) { if (model.teamID != null) model.TeamSummary = new TeamSummaryViewModel(model.teamID);

    return View(model);
}

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.