-1

我对 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>&copy; @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>

单击链接它会重新加载整个页面并清除我的侧面板

4

2 回答 2

0

示例场景

模型

public class Menu
{
    // prop parent for example 
    public List<Parents> parents{ get; set; }
    // prop child
    public List<Childs> child{ get; set; }
    // prop check
    public bool checked { get; set; }
}

控制器

public class Menu: Controller
{
    public ActionResult _MainMenu()
    {
        Menu menu = new Menu();
        menu.Parent = // parent elements from your db context
        menu.Child = // child elements from your db context
        menu.Parent = // check element from your db context

        return PartialView(menu);
    }
}

_MainMenu.cshtml

@model Menu

// same like your layout view, fill here like layout
// for example
@foreach(var parent in Model.Parents)
{
    // generate menus
    ....
 }

_Layout.cshtml

...
<nav>
    <ul id="menux">
         @Html.Action("_MainMenu", "Menu")
    </ul>
</nav>
...

我建议你一个示例代码,你应该修改你想要的数据类型。

于 2013-03-05T13:58:26.300 回答
0

HTTP 是无状态的,这意味着您不持久的任何数据都会在回发之间丢失。如果您想保留该信息,您将需要使用 Javascript 通过 Ajax 将菜单结构发送到服务器或使用基于浏览器的本地存储

于 2013-03-05T12:57:25.620 回答