How to show sub-categories in the Categories list - nopcommerce

How can I show sub-categories right in the Categories list, e.g.:
Root Category #1
Sub Category #1
Sub Category #2
Sub Category #3
Root Category #2
Sub Category #4
Sub Category #5
Sub Category #6
Nopcommerse v2.60

You need to extend CategoryNavigationModel with something like
public IList<CategoryNavigationModel> ChildCategoryNavigationModels { get; set; }
Then in CategoryNavigation action in CatalogController add one more loop for adding sub categories
foreach (var categoryNavigationModel in model)
categoryNavigationModel.ChildCategoryNavigationModels =
GetChildCategoryNavigationModel(new List<Category>(), categoryNavigationModel.Id, currentCategory, 0);
And then in CategoryNavigation.cshtml you can display sub categories within #foreach (var category in Model)
In that way:
<ul>
#foreach (var subCategory in category.ChildCategoryNavigationModels)
{
<li>
<a href="#Url.RouteUrl("Category", new { categoryId = subCategory.Id, SeName = subCategory.SeName })">
#subCategory.Name
</a>
</li>
}
</ul>

Related

How can I pass parameter to a link generated by ActionLink() in ASP.NET MVC?

I have a route that I can access using the following pattern
ResponsesTotals/Show/1
ResponsesTotals/Show/2
.....
.....
ResponsesTotals/Show/n
From a different view, I need to list names and when the user clicks on them I want to direct them to the routes showing above.
Here is what I have tried
#model List<Proj.Model.Survey>
<ul>
#foreach (var survey in Model)
{
<li>#Html.ActionLink(survey.Name, "Show", "ResponsesTotals", new { SurveyId = survey.Id })</li>
}
</ul>
But that for some reason is generating wrong URLs. Here is what is being generated
ResponsesTotals/Show?Length=15
How can I pass a parameter to the link?
Add null for the last parameter so you hit the correct method:
#Html.ActionLink(survey.Name, "Show", "ResponsesTotals", new { SurveyId = survey.Id },null)

MVC 6 Tag Helpers and foreach

What would I give to asp-for property of a label tag helper in order to display items from a collection. The code below generates a compilation error.
#foreach (var item in Model)
{
<label asp-for="item.BookingCode"></label>
}
The # character escapes the default model lambda code. Therefore you can type:
#foreach (var item in Model)
{
<label asp-for="#item.BookingCode"></label>
}
I Have a simple way to do a list and show properties of it.
List<string> razones = new List<string>();
foreach (var item in _context.Reason)
{
razones.Add (item.Description);
}
System.Diagnostics.Debug.WriteLine(razones.Count);

Hide siteMapNode when at particular view

I have siteMapNode inside SiteMap in Web.sitemap and want to hide one of the menu items, when I am at the page .../example.aspx?title=hiding
I tried to hide that menu from example.aspx.cs at Page_Load with smth like:
MenuItemCollection menuItems = Menu.Items;
MenuItem menuItem = new MenuItem();
foreach (MenuItem item in menuItems)
{
if (item.Text == "home")
menuItem = item;
}
menuItems.Remove(menuItem);
but I couldn't find needed item. Also I have tried to add logic into MenuTop_MenuItemDataBound, but it isn't executing at every url change

Umbraco 7: Get fields from same property based on current page

In my Content section I have a property editor (Archetype) that allows to set content for the site independently from the content tree. It looks like this:
I need to display only the sub categories from one category based on what page I'm currently on. What I have now is:
var catItems = Umbraco.Content(1123).categoryItem; //get the Set Content property editor from Content Section
foreach (var item in catItems)
{
foreach (var sub in item.GetValue<ArchetypeModel>("subCatItem"))
{
<div class="tbl_dt">
<p class="offerName">#sub.GetValue("offerName")</p>
<p class="departurePort">#sub.GetValue("departurePort")</p>
</div>
}
}
This is displaying all the sub category items from all categories. It should display only the sub categories based on current page. How can I make the connection between current page and the sub category item? Or it is best to stick with the property editor in the content tree pages?
The issue is not the code, but rather the structure of your Content Tree.
You have nothing to Associate the Category/Offer to the page you want to display it on.
In order to create an the Association between ContentPage & Category/Offer DocType, you could try one of the following:
Keep the Category/Offers as they are, independent of the content tree (i.e. Pool of Categories/Offers etc.)
Then put a MNTP picker (multi-node tree picker) on the DocType that you want to create the Association between (i.e. 'ProductPage').
Each Page where you want to display the Category/Offer(s) you would need to call the MNTP Picker Property to get the NodeId of that specific Category/Offer for
the current page.
Example:
var catNodeId = #CurrentPage.pickerPropertyAliasHere; //this will give us the NodeId of that specific offer selected for the current page, may need to refactor your code to handle parsing etc.
var catItems = #Umbraco.Content(catNodeId).categoryItem; //Get Categories/Offers from the Offer picked for the current page.
foreach (var item in catItems)
{
foreach (var sub in item.GetValue<ArchetypeModel>("subCatItem"))
{
<div class="tbl_dt">
<p class="offerName">#sub.GetValue("offerName")</p>
<p class="departurePort">#sub.GetValue("departurePort")</p>
</div>
}
}
Put your ArcheType property editor on the DocType you want to Associate with.
Then simply call the property for that page and loop over the items again (see example below).
Example:
var catItems = #CurrentPage.categoryItem; //Get Categories/Offers for the current page
foreach (var item in catItems)
{
foreach (var sub in item.GetValue<ArchetypeModel>("subCatItem"))
{
<div class="tbl_dt">
<p class="offerName">#sub.GetValue("offerName")</p>
<p class="departurePort">#sub.GetValue("departurePort")</p>
</div>
}
}
Good Luck
C

What is the best menu for an ASP.Net application?

What do you find to provide the best menu for an ASP.Net 2.0 - 3.5 web application? Suggestions do not have to particularly be ASP.Net controls, but could be other menu's that work well within an ASP.Net web application.
I would like for the suggestions to be options that do not require purchasing or royalty fees. OpenSource suggestions would be even better.
I've found that the most flexible is to use CSS to style an unordered list like this:
<div id="nav_main" >
<ul>
<li id="current">Button 1</li>
<li>Button 2</li>
<li>Button 3</li>
<li>Button 4</li>
<li>Button 5</li>
</ul>
</div>
You'll find many CSS ways to style this kind of list with hover background images, etc.
Now if you are using Webforms and you want to use your sitemap, then I would suggest using a Repeater and NOT use the menu control. You will have the most control of your generating your list this way.
Similarly, if you are using ASP.NET MVC, you can do a foreach on your sitemap to create your list.
This of course is just for simple menus, but it can be expanded to include more complicated menus. I've found the following CSS-styled menu to be very good: http://www.lwis.net/free-css-drop-down-menu/
YUI buttons or menu controls (works with existing HTML):
http://developer.yahoo.com/yui/examples/button/btn_example07.html
An ASP.NET library that wraps these controls very nicely, released in December 2008:
http://www.codeplex.com/YUIAspNet/
JQuery suckerfish menu, works by using ul,li elements:
http://be.twixt.us/jquery/suckerFish.php
I use jQuery Superfish: http://users.tpg.com.au/j_birch/plugins/superfish/. Highly recommended by others as well.
I also like to create unordered lists. It lets the designer be flexible with the menus. They can use their own js+css solution to create drop down menus or style it nicely for static menus. The same html can easily become left hand, across the top, drop down, or even a full site map with css changes.
To that note, I like to store the site map data in a hierarchal data structure and use a recursive lambda to generate it. For an example see this small console app below with its output.
output html
<li>First<li>FirstSub</li><li><a hr
ef="/secondsub.aspx">SecondSub</a></li></li><li><a href="/second.aspx">Second</a
></li>
the source c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SiteMapDemo
{
class MenuItem
{
public Guid ID {get; set;}
public Guid? ParentID{get;set;}
public string Name { get; set; }
public string Path { get; set; }
public int Rank { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<MenuItem> menu = new List<MenuItem>(new[]{
new MenuItem{ID = Guid.NewGuid(), Name = "First", ParentID=null, Path="/", Rank=0},
new MenuItem{ID = Guid.NewGuid(), Name = "Second", ParentID=null, Path="/second.aspx",Rank=1},
});
menu.AddRange(new[] {
new MenuItem{ID = Guid.NewGuid(), Name = "FirstSub", ParentID=menu[0].ID, Path="/firstsub.aspx",Rank=0},
new MenuItem{ID = Guid.NewGuid(), Name = "SecondSub", ParentID=menu[0].ID, Path="/secondsub.aspx",Rank=1},
});
Func<List<MenuItem>, Guid?, string> renderMenu = null;
renderMenu = (menus, Parent) =>
{
var sub = menus.Where(m => m.ParentID == Parent).OrderBy(s=>s.Rank).ToList();
if (sub.Count > 0)
{
StringBuilder sb = new StringBuilder();
sub.ForEach(s => { sb.Append(String.Format("<li>{1}{2}</li>", s.Path, s.Name, renderMenu(menus, s.ID))); });
return sb.ToString();
}
return "";
};
Console.Write(renderMenu(menu, null));
Console.ReadLine();
}
}
}

Resources