Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using MVC SiteMapProvider and DynamicNodeProvider in MVC site for CRUD operations, DynamicNode gets wrong text #135

Closed
farlop opened this issue Mar 13, 2013 · 9 comments

Comments

@farlop
Copy link

farlop commented Mar 13, 2013

When implementing a DynamicNodeProvider like this:

TestDynamicNodeProvider : DynamicNodeProviderBase
{ 
    Entities db = new Entities();

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        // Build value 
        var returnValue = new List<DynamicNode>();

        // Create a node for each album 
        foreach (var equipo in db.GEN_EQUIPO)
        {
            DynamicNode node = new DynamicNode();
            node.Title = equipo.DESCRIPCION;
            node.RouteValues.Add("id", equipo.ID);                
            yield return node;                
        }            
    }
}

Breadcrumbs shows always the first element in collection instead of current one. See an example below:
Breadcrumb should be "Inicio > Equipo > TRONZADORA > Editar Equipo", but it shows TALADRO insteadm, which is the first item in collection (link also is wrong!).

puto mvcsitemap

@neloriand
Copy link

Have the same issue. Appreciate if smb can solve it.

@NightOwl888
Copy link
Collaborator

I just solved a similar issue in the new version. The dynamic node building logic first tries to use the parent ID that you provide, and then falls back to some default logic (which apparently doesn't always work). If you set the ParentKey manually, you should be able to get the relationship to work.

Here is the way it is done in the MvcMusicStore demo:

        public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
        {
            // Create a node for each album
            foreach (var album in storeDB.Albums.Include("Genre"))
            {
                DynamicNode dynamicNode = new DynamicNode();
                dynamicNode.Title = album.Title;
                dynamicNode.ParentKey = "Genre_" + album.Genre.Name;
                dynamicNode.RouteValues.Add("id", album.AlbumId);

                if (album.Title.Contains("Hit") || album.Title.Contains("Best"))
                {
                    dynamicNode.Attributes.Add("bling", "<span style=\"color: Red;\">hot!</span>");
                }

                yield return dynamicNode; 
            }
        }

@farlop
Copy link
Author

farlop commented May 9, 2013

Hi! I tried today your sollution, but it doesn't seem to fit with my problem. Notice that I don't have a parent. My mvc.sitemap file is declared as:

    <mvcSiteMapNode title="Inicio" controller="Home">`
      <mvcSiteMapNode title="Equipo" controller="Equipo">
        <mvcSiteMapNode title="Crear Equipo" action="Create"></mvcSiteMapNode>
        <mvcSiteMapNode title="Detalle Equipo" action="Details" dynamicNodeProvider="MvcApplication2.TestDynamicNodeProvider, MvcApplication2" preservedRouteParameters="id">
          <mvcSiteMapNode title="Editar Equipo" action="Edit" ></mvcSiteMapNode>
          <mvcSiteMapNode title="Eliminar Equipo" action="Delete"></mvcSiteMapNode>
        </mvcSiteMapNode>
      </mvcSiteMapNode>
    </mvcSiteMapNode>

Notice like edit and delete actions are nested inside details (since I can only go to these throught the details action first).

Beside this, based on an answer to my question posted on stackoverflow.com, I have added the preservedRouteParameters option, which now builds the link fine, but the text is still wrong. I attach a new screenshot:

mvcsitemap2

@neloriand
Copy link

@farlop
I solved similar problem with loading all available objects for Details node (in MvcApplication2.TestDynamicNodeProvider), assigning to each of them a unique Id and then creating nested 'edit' and 'delete' node for !each of them.

@NightOwl888
Copy link
Collaborator

@farlop

As far as I am aware, the only node that can exist with no parent is the root node. All of the other logic assumes every node below it will have a parent otherwise the node cannot be correctly found within the map. If that is what is happening in your case, it would explain why the text is not correct - FindSiteMapNode is not returning the correct result.

@farlop
Copy link
Author

farlop commented May 10, 2013

OK. After taking a deep breath I've done some meditation about what I'm trying to achieve. The real problem I had was about the wrong url generated in the SiteMap. This was due to the missing 'preservedRouteParameters' attribute. So, once this is solved, the only thing remaining is how to swap the default "Detalle Equipo" text that appears in the details node to use a custom label ("Tronzadora").

So maybe this is not about generating dynamic nodes for diferent records (as in musicstore)... any idea about how to achieve this?

@NightOwl888
Copy link
Collaborator

If it is in the topmost node you need the title for, there is a SiteMapTitleAttribute that can be used for this purpose. See the MvcMusicStore demo in the repo for an example how to use it.

If not, check out issue #45 for a custom implementation of a Title Provider. This isn't built in, but I am seriously considering adding this as a feature of v4 when I get a chance. But if you need to have it sooner, you can implement it yourself using the VisibilityProvider as a template.

@farlop
Copy link
Author

farlop commented May 13, 2013

This functionality is just what I need! I expect to see it in the future v4. Meanwhile, I will try to implement it by myself. Thanks a lot for helping me to understand better how MvcSiteMapProvider works.

@farlop farlop closed this as completed May 13, 2013
@NightOwl888
Copy link
Collaborator

Ok, I will add this to v4 when I get a chance. I was trying to think of a way to make it more useful - like being able to override any property, not just title. Thus far, I haven't thought of a good way to implement it like that though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants