在_Layout.cshtml中显示一个视图?

时间:2015-06-01 08:33:00

标签: .net asp.net-mvc-5

我已按照本教程生成动态列表菜单http://dotnetawesome.blogspot.se/2014/06/how-to-create-treeview-with-database-data-mvc4-aspnet.html

这是视图

@model List<Project.Models.Treeview>

@{
    ViewBag.Title = "Simple";
}

<h2>Simple Treeview from Database Data</h2>
<div style="border:solid 1px black; padding:10px; background-color:#FAFAFA">
    <div class="treeview">
        @if (Model != null && Model.Count() > 0)
        {
            <ul>
                @Treeview.GetTreeView(Model, Model.FirstOrDefault().ParentCategoryID)
            </ul>
        }
    </div>
</div>

@* Here We need some Jquery code for make this treeview collapsible *@
@section Scripts{
       <script>
            $(document).ready(function () {
                $(".treeview li>ul").css('display', 'none'); // Hide all 2-level ul
                $(".collapsible").click(function (e) {
                    e.preventDefault();
                    $(this).toggleClass("collapse-tree expand-tree");
                    $(this).closest('li').children('ul').slideToggle();
                });
            });
        </script>
         } 

它工作正常,但我没有把它放在一个单独的视图中,我想把它整合到我的主要手册中。我想在下面的代码中显示上面视图中的结果,其中我有文本&#34;显示在这里&#34;。 _Layout.cshtml

            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
Display here!!
                        <li>@Html.ActionLink("Video", "Index", "Video")</li>
                        <li>@Html.ActionLink("Photos", "Index", "Photos")</li>
</ul>
<div> 

我查看过Partial View和Html.RenderAction但不知道该使用什么

修改

文件结构

Controllers
-- TreeviewsController.cs

Models
-- Treeview.cs

Views
Shared
-- _Layout

Treeviews
-- Index.cshtml
-- Create.chhtml
-- Delete.chhtml

使用

进行测试
<li>@{Html.RenderPartial("Index", "Treeviews");}</li>
<li>@{Html.RenderPartial("Treeviews");}</li>

将文件从索引(在视图Treevviews中)重命名为_Treeviews

<li>@{Html.RenderPartial("_Treeviews");}</li>

2 个答案:

答案 0 :(得分:0)

您应该使用Html.RenderPartial("YourTreeViewName")方法。 https://msdn.microsoft.com/en-us/library/dd492503(v=vs.118).aspx

答案 1 :(得分:0)

渲染部分视图的方法

  • Html.RenderPartial - 更快 Html.Partial
  • Html.Partial

使用@{Html.RenderPartial("_partialViewName")}呈现部分视图