how set NavigationMenu title #7181
-
I want set Title to NavigationMenu(set A to B). My code is in NavigationItems.cs. public List<NavigationItemAttribute> GetItems()
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
To be honest, I'm having some difficulty understanding your code. At the beginning of the And, after that, you similarly seem to set the title attribute for Navigation Menu items in the If I had to guess I would guess that the difference between your section headers (Navigation Menu) and your menu items (Navigation Link) results from the difference between the So, I'll ask a couple of things:
Also I want to say that I have never put code into NavigationItems.cs the way you are doing since the file appears to be declarative in nature. Maybe you got this idea from the Dynamic Navigation sample? In any case, to change the menu behavior, I have put code in NavigationModelFactory.cs, and there you are manipulating Navigation Items, not Menu or Link Item Attributes, regardless of which type it is. The actual work of gathering both types of items is done by Navigation Helper. Have you looked at https://github.com/serenity-is/Serenity/blob/master/src/Serenity.Net.Web/Navigation/NavigationHelper.cs ? |
Beta Was this translation helpful? Give feedback.
-
Hi again, I'll try to answer everything, one item at a time. Sorry, this will be quite long, because I learned something while trying to do illustrative examples for you and this answer evolved as I worked on it.
... is it possible that these two instructions are incompatible? What happens if you leave out Once again, I have not worked directly with NavigationMenuAttribute and NavigationLinkAttribute, and my impression was that the NavigationItems.cs was supposed to be declarative syntax. I have never put code there and don't know for sure that you are doing anything wrong. More on this below. Also, to prove that the line If you don't learn anything from these tests, I did see something interesting with Serenity's menu caching, which might be involved, although I don't see how. You could try a similar test to what I am showing (changing a second attribute, for me FullPath worked), and see if makes a difference. I will explain this fully below.
I will try to describe how it works now. First I will try to make sure that you understand what I mean about NavigationItems being "declarative". In all of the generated code for v6 and above, the *Navigation.cs files generated for various modules only have declarative code in them, which looks like this (where Test is a module and the file for the module is called TestNavigation.cs):
For the Administration module, the AdministrationNavigation.cs file looks like this:
Notice that the AdministrationNavigation.cs file has one NavigationMenu() line and several NavigationLink() lines. For both types, you can set a title or caption, although it is done differently for each in this declarative syntax. For the Menu item, as you see, there is a string ("Administration") without an attribute name, but for the Link items, you can use Title="". I am sure that these capabilities correspond, underneath, to the abilities specified by NavigationLinkAttribute versus NavigationMenuAttribute. The NavigationItems.cs file is similarly declarative only, and it serves to group things together on the menu, like this:
The NavigationModelFactory is where all this stuff gets put together, and this is where the coding is done. Yes, I do think this is better, assuming it is available to you. It's definitely important to realize that NavigationModelFactory is where the menu caching is done. When you figure out your actual method. You will not want to run this code over and over if at all possible. That's one reason I think that it is better to make the changes in there, or in the Helper class that it invokes. You will see this in the NavigationModelFactory Create method:
You will see that NavigationHelper is providing a list of NavigationItems, and this would include both links and menu items. Again, the NavigationItem class has a Title member, usable for both types. Your code could leverage getting of the list from NavigationHelper, inside the caching mechanism, like this:
When I did this as a test, I learned a couple of interesting things, which I think might be pertinent to you whether you do this work in the "old" way or the "new" way:
Here is my sample ReviseItem code:
Here is the result: So, again, without making other changes, you could try your current method and add a change to FullPath and see if everything magically works then. But I would still look into making sure you understand the caching of menu items and try to ensure that you aren't doing this work every time every user calls up the menu. As a final recommendation, when you do custom work like this you usually have to provide a way to invalidate the cache if something changes; for example, if your Menu table is edited. But this is not difficult, and once you are sure of how you are doing this we can figure that out pretty easily. |
Beta Was this translation helpful? Give feedback.
-
I need to add further to this... Victor Tomali has explained to me what is going on. To be sure, I should probably know what version of Serenity you are using, but it is mostly likely to be that _Sidebar.cshtml is looking up the correct Localized value for you, and discarding whatever you put in from your Menu table. The reason it is working for your link items would be not because link and menu items are truly different, but because your custom items aren't in the localization tables. But the headers or grouping menu items are not custom, so they are in there and they take precedence. I think that the text keys for looking up an item in the Translations list (look for the ones that start with Navigation) are based on FullPath, which is why it worked for me when I added a change to FullPath; my items were no longer found in the Translations list. I suppose this means that, if you're only using the Menu table for these captions, you could add your items to the Translations list and forget about the custom code. Alternatively, if your Menu table is handling other things for you, you might want to get rid of the items in the Translations list that represent known headers and are interfering with your custom code. But I still recommend thinking about caching etc as mentioned earlier. I hope this helps. Thanks again to Victor for the "boost". |
Beta Was this translation helpful? Give feedback.
I need to add further to this... Victor Tomali has explained to me what is going on. To be sure, I should probably know what version of Serenity you are using, but it is mostly likely to be that _Sidebar.cshtml is looking up the correct Localized value for you, and discarding whatever you put in from your Menu table.
The reason it is working for your link items would be not because link and menu items are truly different, but because your custom items aren't in the localization tables. But the headers or grouping menu items are not custom, so they are in there and they take precedence.
I think that the text keys for looking up an item in the Translations list (look for the ones that start…