我对 MVC 和 C# 真的很陌生,我修改了 _Layout.cshtml 以拥有一个菜单,并且在单击任何菜单项时,我有一个 jquery 函数来根据我单击的父项创建子菜单项。当我单击子菜单中的任何链接时,它会重新加载母版页并清除我不喜欢的子菜单。有人可以帮助我在页面重新加载后动态重新创建相同的子菜单,或者欢迎任何解决该问题的想法好的我创建了一个控制器,我在我的 _Layout 中访问它来创建我的菜单栏
public abstract class MainController : Controller
{
private static HRMenuDataContext db = new HRMenuDataContext();
public static string theVal, toCheck;
public static int num;
public static HRMenuDataContext theData
{
get { return db; }
}
public MainController()
{
//ViewData["Parent"] = from c in theData.psHCMLanguages
// where
// (from m in theData.psHCMMenus
// where
// m.sModule == "AP"
// select m.sMainRef
// ).Distinct().Contains(c.szCode)
// select c;
ViewData["parent"] = theData.theParent("HR");
ViewData["check"] = theData.doCheck(toCheck);
ViewData["Child"] = from c in theData.psHCMLanguages
where
(from m in theData.psHCMMenu_1s
let parent = theVal
where
m.sModule == "HR" &&
m.sSubRef == parent
select
m.sPrompt
).Contains(c.szCode)
select c;
}
}
在我的 _Layout.cshtml 我有这个
@using Menus.Data;
@using Menus.Controllers;
@using System.Linq;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - Persol Systems</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<link href="~/Content/theStyle.css" rel="stylesheet" />
<style type="text/css">
#theSide {
clear:both;
position:relative;
float:left;
left:100px;
bottom:50px;
top:0;
border:1px solid red;
width:150px;
height:600px;
}
</style>
</head>
<body>
@{
string[] name;
char[] sep = { ' ', '/', '\\', '.' };
string mess="";
}
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
</div>
<div class="float-right">
<section id="login">
@Html.Partial("_LoginPartial")
</section>
<nav>
<ul id="menux">
@{
foreach (var c in (IEnumerable<theParentResult>)ViewData["parent"])
{
<li><a>@c.English </a>
@{MainController.theVal = c.szCode;}
<ul class="firstChild">
@{foreach (var d in (IEnumerable<psHCMLanguage>)ViewData["Child"])
{
var data = MainController.theData.doCheck(d.szCode);
if ((int)data == 1)
{
<li><a onclick="theAlert('@d.szCode','@d.English')">@d.English</a>
@{
MainController.theVal = d.szCode;
}
</li>
}
else if (data == 0)
{
name = d.English.Split(sep);
foreach (string nams in name)
{
mess += nams;
}
<li>
<a onclick="theAlert('@d.szCode','@d.English')">@d.English</a>
@*@Html.ActionLink(d.English, mess, c.English)*@
</li>
mess = "";
Array.Clear(name, 0, name.Length);
}
}
}
</ul>
</li>
}
}
</ul>
</nav>
</div>
</div>
</header>
<div id="body">
<div id="theSide">
<ul id="sidePanel">
</ul>
</div>
@RenderSection("featured", required: false)
<section class="content-wrapper main-content clear-fix">
@RenderBody()
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>© @DateTime.Now.Year - Persol Systems</p>
</div>
</div>
</footer>
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
<script type="text/javascript">
function theAlert(theId, theName) {
$("ul#sidePanel").contents().remove();
$("ul#sidePanel ").append("<li id='" + theId + "'><a >" + theId + "</a></li>");
$("li#" + theId + "> a").click(function (e) {
window.location.href("../Home/About");
});
return false;
}
</script>
</body>
</html>
单击链接它会重新加载整个页面并清除我的侧面板