maartenba / MvcSiteMapProvider

An ASP.NET MVC SiteMapProvider implementation for the ASP.NET MVC framework.
Microsoft Public License
538 stars 218 forks source link

Is it possible to get site map path for partial views #426

Closed MohammedAbdulMateen closed 8 years ago

MohammedAbdulMateen commented 8 years ago

If a partial view is rendered by using the _ASP.NET MVC _Html Helpers__, jQuery or AngularJS 1.x then is it possible to get that partial view also in the site map path, I am using the MvcSiteMapNode attribute, I've seen in the site map templates that the partial views are coming as an item in Descendants property in the Model but not in the Children property, replacing the Children with Descendants is showing the partial view also in the site map menu i.e. @Html.MvcSiteMap().Menu() but not in site map path i.e. @Html.MvcSiteMap().SiteMapPath().

Is there any way to achieve this.

Thanks

NightOwl888 commented 8 years ago

Your question is unclear. Partial views are a feature of MVC that we do not use. I don't believe that you can access them from within an HTML helper, but that question would be better suited to StackOverflow.

Could you post the code that you are using to make your question more clear?

MohammedAbdulMateen commented 8 years ago

To mimic the scenario when I render a partial view let's say SportsArticle inside a view called Article

[MvcSiteMapNode(Title = "Sports Article", Key = "SportsArticle", ParentKey = "Article")]
public ActionResult SportsArticle()
{
    return this.PartialView("_SportsArticle");
}

By using @Html.MvcSiteMap().SiteMapPath() I get the following site map path and content

Home > News > Article Article

Sports Article

but conditionally (i.e. for a set of partial views) I have to render the site map path as, Home > Article > Sports Article

and if I go to another link from SportsArticle partial view, I have to render the site map path as, Home > Sports Article > Add

Currently I am thinking of going through https://github.com/maartenba/MvcSiteMapProvider/wiki/Defining-sitemap-nodes-using-IDynamicNodeProvider and then take a dictionary to store all the set of partial views for which I need to process in this way and perhaps try to get current route info i.e. current partial view name from the route table and if I have a match with my dictionary then I will return a DynamicNode else nothing

Will it work out this way if I attach the provider using MvcSiteMapNode to the partial views that I want.

Thanks

NightOwl888 commented 8 years ago

The default behavior of the HTML helpers is not context-aware, so it does not behave differently on a partial view as on a regular view.

However, you could use the FilteredSiteMapNodeVisibilityProvider (or a custom visibility provider) along with named HTML helpers to affect the visibility within a certain HTML helper instance.

For example:

@Html.MvcSiteMap().SiteMapPath(new { name = "SportsArticlePartialView" })
<mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="News" controller="News" action="Index" visibility="!SportsArticlePartialView">
        <mvcSiteMapNode title="Article" controller="Article" action="Index">
            <mvcSiteMapNode title="Sports Article" controller="Article" action="Sports" />
        </mvcSiteMapNode>
    </mvcSiteMapNode>
</mvcSiteMapNode>

So, if navigating to the page /Article/Sports, the SiteMapPath named SportsArticlePartialView will render

Home > Article > Sports Article

You could then just add a different named HTML helper to each partial view.

Alternatively, you can control how the SiteMapPath renders by passing it the key of a different starting node (the right-most node) than the current page.

@Html.MvcSiteMap().SiteMapPath((string)null, "ArticleKey")
MohammedAbdulMateen commented 8 years ago

Thanks for the reply, we are currently moving our app to client side routing using AngularJS 1.x which in turn calls ASP.NET MVC Routes, I will try to check what you've suggested when I get the chance to and will update the thread at a later point about what I went with.

MohammedAbdulMateen commented 8 years ago

Unfortunately the project I was working on was stopped and I didn't get a chance to try this out, anyway thanks a lot.