You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC分类模块:关联Menu表展示及用户吸引元素优化咨询

ASP.NET MVC分类板块优化方案

一、优先突出的核心元素

  • 当前活跃分类:让用户清晰定位自己所在分类,减少导航认知成本
  • 分类内容规模:显示分类下的内容数量,直观体现内容丰富度,吸引用户点击
  • 推荐/热门分类标识:给高流量、官方推荐的分类添加视觉标识,引导用户关注
  • 有效跳转链接:替换无效的#为分类实际URL,确保用户能直接访问分类内容

二、具体实现方法

1. 高亮当前活跃分类

通过路由参数获取当前访问的分类ID,与遍历的分类做匹配,为匹配项添加高亮样式:

修改_category.cshtml视图代码:

@{
    // 从路由中获取当前分类ID,根据实际路由规则调整
    var currentCategoryId = ViewContext.RouteData.Values["categoryId"]?.ToString();
}

@foreach (var item in Model)
{
    var isActive = currentCategoryId == item.KategoriId.ToString();
    // 拼接高亮样式类
    var navLinkClass = isActive ? "nav-link dropdown-toggle active" : "nav-link dropdown-toggle";
    var dropdownItemClass = isActive ? "dropdown-item active" : "dropdown-item";

    <li class="nav-item dropdown @(isActive ? "active" : "")">
        <a class="@navLinkClass" type="button" id="dropdownMenuButton_@item.KategoriId" data-bs-toggle="dropdown" aria-expanded="false">
            @item.Menuler.MenuAdi
            <!-- 推荐分类添加徽章标识 -->
            @if(item.IsRecommended) {
                <span class="badge bg-primary ms-2">推荐</span>
            }
        </a>

        <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton_@item.KategoriId">
            <li>
                <!-- 替换#为真实分类跳转链接,假设路由为/Category/{id} -->
                <a class="@dropdownItemClass" href="@Url.Action("Index", "Category", new { id = item.KategoriId })">
                    @item.kategoriAdi
                    <!-- 显示分类下内容数量 -->
                    <span class="badge bg-secondary float-end">@item.ContentCount</span>
                </a>
            </li>
        </ul>
    </li>
}

2. 添加分类内容数量统计

在数据查询中统计每个分类下的内容数量(假设内容表为Contents,外键关联KategoriId):

修改数据模型代码:

public static veri.Kategoriler[] deneme
{
    get
    {
        using (burakEntities db = new burakEntities())
        {
            return db.Kategorilers
                     .Include(p => p.Menuler)
                     // 统计分类下内容数量
                     .Select(p => new veri.Kategoriler
                     {
                         KategoriId = p.KategoriId,
                         kategoriAdi = p.kategoriAdi,
                         kategorilertarih = p.kategorilertarih,
                         KategoriAktif = p.KategoriAktif,
                         Menuler = p.Menuler,
                         ContentCount = db.Contents.Count(c => c.KategoriId == p.KategoriId)
                     })
                     .OrderByDescending(p => p.kategorilertarih)
                     .Where(p => p.KategoriAktif == "true"
                               && p.Menuler.KategoriMi == "true"
                               && p.Menuler.MenuMi == "false"
                               && p.Menuler.Menulerid == p.Menulerid)
                     .ToArray();
        }
    }
}

提示:如果Kategoriler实体没有ContentCount属性,建议新建ViewModel封装数据(避免直接修改实体类),或者给实体新增对应字段。

3. 标记推荐/热门分类

在Kategoriler表新增IsRecommended字段(bit类型),用于标识推荐分类,查询时包含该字段,即可在视图中展示对应标识。

4. 强化视觉样式

通过CSS增强高亮和标识的视觉效果:

.nav-link.active, .dropdown-item.active {
    color: #fff !important;
    background-color: #0d6efd !important;
    font-weight: bold;
}

.badge.bg-primary {
    font-size: 0.7em;
}

.badge.bg-secondary {
    font-size: 0.8em;
}

三、额外优化建议

从当前输出效果看,存在同一Menu下重复生成下拉按钮的情况,建议先按MenuAdi分组再展示分类,避免界面冗余。

内容的提问来源于stack exchange,提问作者Berkay Özbaş

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 02:17:03