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;
}