1
我想實現在MvcSiteMapProvider類似如下的HTML助手:使用MvcSiteMap如何創建到下一個和前一個節點的鏈接?
Html.MvcSiteMap().Previous()
Html.MvcSiteMap().Next()
不過,我很新的他們的API,是有可能做到這一點,如果是的話,怎麼樣?
我想實現在MvcSiteMapProvider類似如下的HTML助手:使用MvcSiteMap如何創建到下一個和前一個節點的鏈接?
Html.MvcSiteMap().Previous()
Html.MvcSiteMap().Next()
不過,我很新的他們的API,是有可能做到這一點,如果是的話,怎麼樣?
您可以通過構建自定義HTML助手來完成此操作。我已經在GitHub上提供answered this question並提供working demo project,但我在這裏複製以供參考。
上下文檔的邏輯看起來像這樣,剩下的代碼大部分是樣板模板HTML輔助代碼。
private static ISiteMapNode GetNextNode(ISiteMapNode startingNode, IDictionary<string, object> sourceMetadata)
{
ISiteMapNode nextNode = null;
if (startingNode.HasChildNodes)
{
// Get the first child node
nextNode = startingNode.ChildNodes[0];
}
else if (startingNode.ParentNode != null)
{
// Get the next sibling node
nextNode = startingNode.NextSibling;
if (nextNode == null)
{
// If there are no more siblings, the next position
// should be the parent's next sibling
var parent = startingNode.ParentNode;
if (parent != null)
{
nextNode = parent.NextSibling;
}
}
}
// If the node is not visible or accessible, run the operation recursively until a visible node is found
if (nextNode != null && !(nextNode.IsVisible(sourceMetadata) || nextNode.IsAccessibleToUser()))
{
nextNode = GetNextNode(nextNode, sourceMetadata);
}
return nextNode;
}
private static ISiteMapNode GetPreviousNode(ISiteMapNode startingNode, IDictionary<string, object> sourceMetadata)
{
ISiteMapNode previousNode = null;
// Get the previous sibling
var previousSibling = startingNode.PreviousSibling;
if (previousSibling != null)
{
// If there are any children, go to the last descendant
if (previousSibling.HasChildNodes)
{
previousNode = previousSibling.Descendants.Last();
}
else
{
// If there are no children, return the sibling.
previousNode = previousSibling;
}
}
else
{
// If there are no more siblings before this one, go to the parent node
previousNode = startingNode.ParentNode;
}
// If the node is not visible or accessible, run the operation recursively until a visible node is found
if (previousNode != null && !(previousNode.IsVisible(sourceMetadata) || previousNode.IsAccessibleToUser()))
{
previousNode = GetPreviousNode(previousNode, sourceMetadata);
}
return previousNode;
}