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.
Html.ActionLinkcode that you're using to generate the link? Does the route work if you manually type in one that should work?