Get the App
SLTechnology News&Howtos  ›  Database  › 

How to create dynamic menus in ASP. Net core razor page with Web Api

Shulou Source: shulou.com Published: 2022-06-01 04:48:30 10月04日 Update

我得到一个错误当我添加一个MenuList进入菜单。 计算机科学,因为我的MySql表中没有MenuList那里。 但我无法添加MenuList菜单。 cs,这意味着我不能创建一个动态菜单,任何人在这种情况下有解决方案吗? 请帮助我,我被困在这里大约一个星期。

MySql表:

CREATE TABLE Menusss( MenuId int not null auto_increment, MenuName varchar(250), ParentId int, ActiveNo int);

Menus.cs:

public class Menus{ [Key] public int MenuId { get; set; } public string MenuName { get; set; } public int? ParentId { get; set; } public int ActiveNo { get; set; } public List MenuList { get; set; } = new List();}

MenusController.cs:

[HttpGet]public ActionResult GetMenus(){ List menuList = new List(); foreach (Menus m in _context.menus.ToList()) { menuList.Add(m); } List menuTree = GetMenuTree(menuList, null); return menuTree;}private List GetMenuTree(List list, int? parentId){ return list.Where(x => x.ParentId == parentId).Select(x => new Menus() { MenuId = x.MenuId, MenuName = x.MenuName, ParentId = x.ParentId, ActiveNo = x.ActiveNo, MenuList = GetMenuTree(list, x.MenuId) }).ToList();}

MY.js:

$(document).ready(function () { $.ajax({ url: '', method: 'get', dataType: 'json', success: function (data) { buildMenu($('#menu'), data); $('#menu').menu(); }}); function buildMenu(parent, items) { $.each(items, function () { var li = $("" + this.MenuName + ""); if (this.ActiveNo == 0) { li.addClass('ui-state-disabled'); } li.appendTo(parent); if (this.MenuList && this.MenuList.length > 0) { var ul = $(""); ul.appentTo(li); buildMenu(ul, this.MenuList); } }); }});

Tags: Menu dynamic next situation meaning solution week Science solution computer error help Razor Core Page Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info vpn Linux Docker MySQL