当我将menulist添加到menus.cs时,出现了一个错误,因为在mysql表中没有menulist。但是我不能在menus.cs中添加菜单列表,这意味着我不能正确地创建动态菜单,有人能解决这个问题吗?请帮帮我,我被困在这里大约一个星期了。
mysql表:
CREATE TABLE Menusss(
MenuId int not null auto_increment,
MenuName varchar(250),
ParentId int,
ActiveNo int
);
菜单.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<Menus> MenuList { get; set; } = new List<Menus>();
}
menuscontroller.cs:
[HttpGet]
public ActionResult<List<Menus>> GetMenus()
{
List<Menus> menuList = new List<Menus>();
foreach (Menus m in _context.menus.ToList())
{
menuList.Add(m);
}
List<Menus> menuTree = GetMenuTree(menuList, null);
return menuTree;
}
private List<Menus> GetMenuTree(List<Menus> 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();
}
我的.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 = $("<li>" + this.MenuName + "</li>");
if (this.ActiveNo == 0) {
li.addClass('ui-state-disabled');
}
li.appendTo(parent);
if (this.MenuList && this.MenuList.length > 0) {
var ul = $("<ul></ul>");
ul.appentTo(li);
buildMenu(ul, this.MenuList);
}
});
}
});
暂无答案!
目前还没有任何答案,快来回答吧!