I'm using VS2013 Preview on W8.1 under project MVC 5 (.NET 4.5 not 4.5.1) and I've been researching for the past few hours trying all sorts of things and it seems I just don't get what I'm missing.
I'm working on a school project by building a forum and I want the URL to be hierarchical, i.e.
localhost:1234/Forum/Science/Physics/String%20Theory
.
This is the RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{action}/{*title}",
defaults: new { controller = "Home", action = "Index", title = UrlParameter.Optional }
);
}
Controller:
public ActionResult Index()
{
return View(db.Categories.Where(x => x.ParentId == null).ToList());
}
public ActionResult Forum(string parentId)
{
return View("Index", db.Categories.Where(x => x.ParentId == parentId));
}
And view (which is the index page):
@foreach (var item in Model)
{
<div class="CatLevel0">
<h2>@Ajax.ActionLink(item.Title, "Forum", new { parentId = item.Id, title = item.Title }, new AjaxOptions() { HttpMethod = "POST" })</h2>
<h4>@Html.DisplayFor(modelItem => item.Description)</h4>
</div>
}
Herein lies the problem. The link above (e.g. "Science") directs to:
"http://localhost:1234/Forum/Science?parentId=b8bd9ded-7284-462d-b0cc-d8ce09717b8a"
,
and the 2nd level after being forwarded to "Science" and being redirected to "Social Sciences" I get:
"http://localhost:1234/Forum/Social%20Sciences?parentId=2a9f1c24-c6d4-44ab-b000-3268f38794f3"
.
So not only do I get the redundant GUID in the querystring (which I don't want!), but I also lose the precursor "Science" in "~/Forum/Science/Social%20Sciences";
In a few other SO questions it was noted that Ajax.ActionLink requires jquery unobtrusive ajax, it renders correctly on my end judging by the network tab in Chrome Developer Tools.
Update: I managed to fix the issue @TimothyWalters mentioned, using the following:
Controller:
public ActionResult Forum(string parentId, string title)
{
TempData["fullTitle"] = title + "/";
return View("Index", db.Categories.Where(x => x.ParentId == parentId));
}
View:
@foreach (var item in Model)
{
<div class="CatLevel0">
@*<h2>@Html.ActionLink(item.Title, "Forum", new { parentId = item.Id, title = item.Title })</h2>*@
<h2>@Ajax.ActionLink(item.Title, "Forum", new { parentId = item.Id, title = TempData["fullTitle"] + item.Title }, new AjaxOptions() { HttpMethod = "POST" })</h2>
<h4>@Html.DisplayFor(modelItem => item.Description)</h4>
</div>
}
So now I have http://localhost:5465/Forum/Science/Social%20Sciences?parentId=2a9f1c24-c6d4-44ab-b000-3268f38794f3
, which leaves the issue of the GUID in querystring to be handled.
Update2:Ugh - now I get this: http://localhost:5465/Forum/Science/Social%20Sciences/Science/Social%20Sciences?parentId=2a9f1c24-c6d4-44ab-b000-3268f38794f3
. . . .