使用mvc+ef从数据库中读取树形菜单自己写的小例子

Models页面代码:
 
    

public class TB_MenuList
{
///


/// 主键
///

public int ID { get; set; }
///
/// 菜单标题
///

public string Title { get; set; }
///
/// 菜单英文标题(备用)
///

public string Title_Eng { get; set; }
///
/// 用户可见权限(0代表无需无权限)
///

public int DisplayPermission { get; set; }
///
/// 父级ID(用来保存上一节点ID,0代表顶级)
///

public int ParentID { get; set; }
///
/// 菜单链接地址
///

public string URL { get; set; }
///
/// 是否在新页面打开
///

public int IsNewPage { get; set; }

}

public class TB_MenuListDBContext : DbContext
{
public DbSet TB_MenuList { get; set; }
}

controllers页面:
 
    

private TB_MenuListDBContext db = new TB_MenuListDBContext();

//
// GET: /TB_MenuList/

public ActionResult Index()
{
return View(db.TB_MenuList.ToList());
}


//
// GET: /TB_MenuList/Details/5

public ActionResult Details(int id = 0)
{
TB_MenuList tb_menulist = db.TB_MenuList.Find(id);
if (tb_menulist == null)
{
return HttpNotFound();
}
return View(tb_menulist);
}

//
// GET: /TB_MenuList/Create

public ActionResult Create()
{
return View();
}

//
// POST: /TB_MenuList/Create

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(TB_MenuList tb_menulist)
{
if (ModelState.IsValid)
{
db.TB_MenuList.Add(tb_menulist);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(tb_menulist);
}

//
// GET: /TB_MenuList/Edit/5

public ActionResult Edit(int id = 0)
{
TB_MenuList tb_menulist = db.TB_MenuList.Find(id);
if (tb_menulist == null)
{
return HttpNotFound();
}
return View(tb_menulist);
}

//
// POST: /TB_MenuList/Edit/5

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(TB_MenuList tb_menulist)
{
if (ModelState.IsValid)
{
db.Entry(tb_menulist).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tb_menulist);
}

//
// GET: /TB_MenuList/Delete/5

public ActionResult Delete(int id = 0)
{
TB_MenuList tb_menulist = db.TB_MenuList.Find(id);
if (tb_menulist == null)
{
return HttpNotFound();
}
return View(tb_menulist);
}

//
// POST: /TB_MenuList/Delete/5

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
TB_MenuList tb_menulist = db.TB_MenuList.Find(id);
db.TB_MenuList.Remove(tb_menulist);
db.SaveChanges();
return RedirectToAction("Index");
}

protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}

///


/// 页面左侧部分的树节点,使用ajax
///

///
///
public ActionResult GetMenuListByParentID(int ParentID)
{
var tb_menulist = from m in db.TB_MenuList select m;
tb_menulist = tb_menulist.Where(s => s.ParentID.Equals(ParentID));
return PartialView("Partial_GetMenuListByParentID", tb_menulist);
}

///
/// 页面的左侧部分
///

///
public ActionResult Layout_Index()
{
var tb_menulist = from m in db.TB_MenuList select m;
tb_menulist = tb_menulist.Where(s => s.ParentID.Equals(0));
return View(tb_menulist);
}

///
/// 页面的首页
///

///
public ActionResult Default()
{

return View();
}

///
/// 页面的顶部页面
///

///
public ActionResult Top()
{
ViewBag.Name = User.Identity.Name;
return View();
}

Default.cshtml页面代码( 注:由于frameset不能应用在body标签之间,使用必须加 Layout = null;此代码,并且再次页面不能出现别的html标签,如div标签等,没有一一实验,这也是导致mvc无法显示frameset的原因 ):
 
    

@{
Layout = null;
}






Layout_Index.cshtml页面代码(使用ajax读取点击的标签下的叶节点):
 
    

@model IEnumerable



@{int i=0;
foreach (var item in Model) {
i++;


}
}

Partial_GetMenuListByParentID.cshtml页面代码( 由于是需要查找叶子节点,使用使用的是创建部分视图,网上说部分视图一般放在views的shared文件夹中,所以我也就创建在了那里,步骤:右键shared-添加-视图-输入名字并勾选创建部分视图-确定,此处的名字和GetMenuListByParentID方法的中返回的PartialView方法中的第一个参数名字相同 ):
 
    

@model IEnumerable


你可能感兴趣的:(MVC)