0

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 . . . .

4

1 回答 1

0

如果您不希望在查询字符串中使用 GUID,请停止将其放在那里。如果你不打算在那里拥有它,你将需要一些可靠的方法来从你的路径中提取意义。

您明确告诉它将 GUID 放入查询字符串中,并在您的视图中包含parentId您对ActionLink(). 删除该参数,您将不再有 GUID。

要从您的路径中提取意义,您需要一种方法将“科学/物理/弦理论”转变为您之前通过 GUID 找到的父级的“弦理论”子级。

var parts = title.Split('/');
var categories = db.Categories
    .Where(c => parts.Contains(c.Title))
    .ToList();

// start at the root
var category = categories.Where(c => c.ParentId == null && c.Title == parts[0]);

// look for anything past the root level (starting at index 1 not 0)
for (var i = 1; i < parts.Length; i++)
{
    category = categories.Where(c => c.ParentId == category.Id && c.Title == parts[i]);
}

// use category to create new view
return View(category);
于 2013-09-12T00:20:21.957 回答