1

我使用 MvcSiteMapProvider 在 Asp.net MVC 4 中创建树视图导航器

我有 2 个链接,例如:

~/Home/Article/{id} 和 ~/Home/Gallery/{id}

我的 Treeview 喜欢:主页 -> 文章 -> 图库

我在控制器上使用了动态代码

    [MvcSiteMapNode(Title = "Article", ParentKey = "Home", Key="Article", PreservedRouteParameters="id")]
    public ActionResult Article(int id)
    {
        ViewBag.Id = id; 
        return View();
    }
    [MvcSiteMapNode(Title = "Gallery", Key="Gallery" ParentKey = "Article", PreservedRouteParameters="id")]
    public ActionResult Gallery(int id)
    {
        ViewBag.id = id;
        return View();
    } 

所以它运行成功,但问题是当我有 ~/Home/Article/123 并且我去 ~/Home/Gallery/456

接下来我点击treeview返回文章,它在文章中设置了错误的ID参数,它为文章的ID设置了Gallery的ID,如下所示:~/Home/Article/456。

有人有求解器吗?对不起我的英语,它很糟糕。

4

1 回答 1

0

您可以显式设置参数的名称。

例如。

[MvcSiteMapNode(Title = "Article", ParentKey = "Home", Key="Article", PreservedRouteParameters="ArticleId")]
public ActionResult Article(int ArticleId)
{
    ViewBag.Id = ArticleId; 
    return View();
}

[MvcSiteMapNode(Title = "Gallery", Key="Gallery" ParentKey = "Article", PreservedRouteParameters="GalleryId")]
public ActionResult Gallery(int GalleryId)
{
    ViewBag.id = GalleryId;
    return View();
} 

然后:

/Home/Article/123

/Home/Gallery?GalleryId=456&ArticleId=123

于 2013-04-27T04:37:40.447 回答