简体   繁体   English

如何在ASP.NET中的Layout.CSHTML中创建下拉导航栏菜单项

[英]How to Create Dropdown Navbar Menu Item in Layout.CSHTML in ASP.NET

I am new to ASP.NET and I am trying to figure out how to create a nav bar menu items with drop down capability. 我是ASP.NET的新手,我试图弄清楚如何创建具有下拉功能的导航栏菜单项。 I am using this theme in particular. 我特别使用这个主题。 In their sample code they demonstrate the drop down functionality, but in my default MVC application I am using _Layout.cshtml to create my nav bar. 在他们的示例代码中,他们演示了下拉功能,但是在我的默认MVC应用程序中,我正在使用_Layout.cshtml创建导航栏。 I am having difficulty figuring out the correct syntax. 我很难弄清楚正确的语法。

I have implemented section s within my Index.cshtml page which I would like the first nav bar menu item drop downs to go to accordingly. 我已经在Index.cshtml页面中实现了s section ,我希望第一个导航栏菜单项下拉至相应位置。 How may I accomplish this? 我该怎么做?

Index.cshtml Index.cshtml

<section id="item 1">...</section>
<section id="item 2">...</section>
<section id="item 3">...</section>

_Layout.cshtml _Layout.cshtml

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("My Application", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                @*<li>@Html.ActionLink("Index", "Index", "Home")</li>*@
                <li class="dropdown">
                    <a class="dropdown-toggle" role="button" aria-expanded="false" data-toggled="dropdown">@Html.ActionLink("Home", "Index", "Home")</a>
                <ul class="dropdown-menu" role="menu">
                    <li>Action 1</li>
                    <li><a href="#">Another action</a></li>
                    <li><a href="#">Something else here</a></li>
                </ul>
                </li>
                <li>@Html.ActionLink("About", "About", "Home")</li>
                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
            </ul>
        </div>
    </div>
</div>

Thanks in advance! 提前致谢!

EDIT: 编辑:

I've updated my _Layout.cshtml which shows the dropdown and the list items do navigate to my Index page, but from here how can I specifically direct each list item to point to each section of Index.cshtml page (ie item 1 , item 2 , or item 3 ? 我已经更新了_Layout.cshtml,其中显示了下拉列表,并且列表项确​​实导航到我的索引页面,但是从这里我如何才能具体地将每个列表项指向Index.cshtml页面的每个部分(即item 1item 2item 3

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("My Application", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                @*<li>@Html.ActionLink("Index", "Index", "Home")</li>*@
                <li class="dropdown">
                    <a class="dropdown-toggle" role="button" aria-expanded="false" href="#" data-toggle="dropdown">Home <span class="caret"></span></a>
                    <ul class="dropdown-menu" role="menu">
                        <li>@Html.ActionLink("Item 1", "Index", "Home", null, new { id = "item 1" })</li>
                        <li>@Html.ActionLink("Item 2", "Index", "Home", null, new { id = "item 2" })</li>
                        <li>@Html.ActionLink("Item 3", "Index", "Home", null, new { id = "item 3" })</li>
                    </ul>
                </li>
                <li>@Html.ActionLink("About", "About", "Home")</li>
                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
            </ul>
        </div>
    </div>
</div>

Here is another link I've referenced. 这是我引用的另一个链接

My HomeController.cs for ActionResult Index 我的HomeController.cs用于ActionResult Index

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

This issue has less to do with asp.net than with html. 这个问题与asp.net的关系比与html的关系少。 In the link you provided you can view source on all elements on the page. 在您提供的链接中,您可以查看页面上所有元素的源代码。 Viewing the source on the navigation bar gives the html below. 在导航栏上查看源代码将显示以下html。

You just need to put this html somewhere in your layout.cshtml page and reference their css files so that the styling comes into effect (either download the css or link it from a hosted version someone online) 您只需要将此html放在您的layout.cshtml页面中的某处,并引用其CSS文件,即可使样式生效(下载CSS或从在线的托管版本中链接它)

you can use a link tag to reference css on your html page: 您可以使用链接标记在HTML页面上引用CSS:

<link rel="stylesheet" type="text/css" href="mystyle.css">

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
      </ul>
    </div>
  </div>
</nav>

The solution by PussInBoots here resolves the issue. 通过PussInBoots该解决方案在这里解决问题。

@Html.ActionLink("Some link text", "MyAction", "MyController", protocol: null, hostName: null, fragment: "MyAnchor", routeValues: null, htmlAttributes: null)

Where MyAnchor will be the section id from the original question. 其中MyAnchor将是原始问题的部分ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM