如何在ASP.NET MVC / C#/ LINQ / SQL中的菜单上显示类别和子类别

时间:2015-07-06 07:41:12

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-5

我想将类别和子类别填充为菜单,然后通过单击子类别菜单来显示产品信息。

我有一个类别,子类别的模型类,这样的产品,

 public class Category
    {
        public Category()
        {
           Products = new HashSet<Product>();
           SubCategories = new HashSet<SubCategory>();
        }
        public int CategoryId { get; set; }

        public string CategoryName { get; set; }



        public virtual ICollection<SubCategory> SubCategories { get; set; }

        public virtual ICollection<Product> Products { get; set; }
    }

public class SubCategory
    {
        public SubCategory()
        {
            Products = new HashSet<Product>();
        }

        public int SubCategoryId { get; set; }
        public string SubCategoryName { get; set; }

        public int CategoryId { get; set; }

        public virtual Category Category { get; set; }
        public virtual ICollection<Product> Products { get; set; }

    }

public class Product
    {

        public int ProductId { get; set; }
        public string ProductName { get; set; }



        public int CategoryId { get; set; }

        public int SubCategoryId { get; set; }

        public virtual Category Category { get; set; }

        public virtual SubCategory SubCategory { get; set; }

    }

我在数据库中有子类别项目。

|CategoryName    |SubCategyName
Computer           HP
Computer          Lenovo 
Mobile            Samsung
Mobile            Apple

菜单视图但不正确:

@model IEnumerable<mTest.Models.SubCategory>



<div>
    <ul>
            @foreach (var category in Model)
            {
                <li class="active has-sub"><a href="#"><span>@category.Category.CategoryName</span></a>
                    <ul>
                        @foreach (var subcategory in Model)
                        {
                            <li class="has-sub"><a href="/Products/show?subcategory=@category.SubCategoryName"><span>@subcategory.SubCategoryName</span></a></li>

                        }
                    </ul>

                </li>
            }
      </ul>
</div>

结果如何:

|Computer=>HP
         =>Lenovo
|Computer=>HP
         =>Lenovo

|Mobile  =>Samsung
         =>Apple
|Mobile  =>Samsung
         =>Apple

但我想要这个。

|Computer=>HP
         =>Lenovo

|Mobile  =>Samsung
         =>Apple

如何获得这个?

2 个答案:

答案 0 :(得分:1)

如果您不想更改模型和View,可以这样写:

@foreach (var category in Model.Select(x => x.Category).Distinct())
{
    <li class="active has-sub">
        <a href="#"><span>@category.CategoryName</span></a>
        <ul>
            @foreach (var subcategory in Model.Where(x => category.Id == x.CategoryId))
            {
                <li class="has-sub"><a href="/Products/show?subcategory=@category.SubCategoryName"><span>@subcategory.SubCategoryName</span></a></li>

            }
        </ul>

    </li>
}

但我建议您使用ViewModel IEnumerable<mTest.Models.Category>类型,而不是IEnumerable<mTest.Models.SubCategory>。它会更干净。

像这样:

@model IEnumerable<mTest.Models.Category>

<div>
    <ul>
            @foreach (var category in Model)
            {
                <li class="active has-sub"><a href="#"><span>@category.CategoryName</span></a>
                    <ul>
                        @foreach (var subcategory in category.SubCategories)
                        {
                            <li class="has-sub"><a href="/Products/show?subcategory=@subcategory.SubCategoryName"><span>@subcategory.SubCategoryName</span></a></li>

                        }
                    </ul>

                </li>
            }
      </ul>
</div>

答案 1 :(得分:-1)

您应该更改模型以使用类别列表。

@model IEnumerable<mTest.Models.Category>
    ...
    @foreach (var category in Model)
    {
        ...                
        @foreach (var subcategory in category.SubCategories)
        {
            ...
        }
        ...
    }

假设填充了每个类别的子类别列表。