2014-09-24 42 views
1

我有一個動態節點提供程序,它與我的站點地圖配置一起在下面複製。當我進入我的網址/ Account/Edit/1475時,麪包屑顯示「Home> Accounts> [Incorrect Account Name]> Edit」。它顯示的帳號名稱與網址1475中的'accountId'不同。我認爲這是由於'preservedRouteParameter = accountId'導致它匹配錯誤的節點。這是正確的嗎?ASP.NET MVC站點地​​圖提供程序 - 動態節點下的非動態節點

我是否需要爲我的站點地圖中的帳戶編輯節點創建另一個DynamicNodeProvider?我開始沿着這條路走下去,但是我需要爲大多數節點創建一個單獨的動態節點提供程序,所以我認爲我必須做錯某些事情。有沒有我在配置中丟失的東西?

public class AccountDynamicNodeProvider : DynamicNodeProviderBase 
{ 
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node) 
    { 
     using (var entities = new Entities()) 
     { 
      foreach (var account in entities.TAccounts) 
      { 
       var dynamicNode = new DynamicNode("Account_" + account.AccountId, account.AccountName); 
       dynamicNode.RouteValues.Add("accountId", account.AccountId); 

       yield return dynamicNode; 
      } 
     } 
    } 
} 

Mvc.sitemap:

<mvcSiteMapNode title="Home" controller="Home" action="Index"> 
    <mvcSiteMapNode title="Accounts" controller="Account" action="Index"> 
    <mvcSiteMapNode title="Detail" controller="Account" action="Details" dynamicNodeProvider="my.test.namespace.AccountDynamicNodeProvider, my.assembly"> 
     <mvcSiteMapNode title="Edit" controller="Account" action="Edit" preservedRouteParameters="accountId" /> 
     </mvcSiteMapNode> 
    </mvcSiteMapNode> 
    </mvcSiteMapNode> 
</mvcSiteMap> 

這是正在使用的路線:

routes.MapRoute(
    name: "Account", 
    url: "Account/{action}/{accountId}", 
    defaults: new { controller = "Account", action = "Details" } 
); 

回答

0

使用SiteMapTitleAttribute使用preservedRouteParameters時動態地更改標題。

[SiteMapTitle("MyTitle", Target = AttributeTarget.ParentNode)] 
public ViewResult Edit(int accountId) { 
    var account = _repository.Find(accountId); 

    // MyTitle is a string property of 
    // the account model object. 
    return View(account); 
} 

或者

[SiteMapTitle("MyTitle", Target = AttributeTarget.ParentNode)] 
public ViewResult Edit(int accountId) { 
    ViewData["MyTitle"] = "This will be the title"; 

    var account = _repository.Find(accountId); 
    return View(account); 
} 

一般來說,配置CRUD操作的時候,最好使用preservedRouteParameters一路。但是走這條路線時需要注意的是,您需要手動修復標題和節點可見性。

CRUD操作(除了可能是添加新)通常不會出現在Menu或SiteMap中,而是通常在頁面上創建一個列表或表以動態生成每條記錄的命令。所以你唯一需要擔心的就是SiteMapPath,爲此你可以使用preservedRouteParameters。

查看How to Make MvcSiteMapProvider Remember a User's Position中的強制匹配演示。

+0

這很好。謝謝! – 2014-09-26 00:25:49