How to insert “File1.cshtml” into a section in mai

2019-08-03 22:13发布

Ok, im probably doing this wrong... But in my main _Layout I have this:

<div id="navigation">
            @RenderSection("Navigation")
        </div>

which points to this in my Index.cshtml view:

@section Navigation{
  <-- Need this to point to Views/Shared/Navigation.cshtml -->
}

But I don't want to have a huge file with all my code in it, so I need to know how to point to a file called "Navigation.cshtml" inside of this section - basically so I have all my sections in separate, independent files.

I tried just doing @RenderPage("Navigation.cshtml") in the _Layout instead of @RenderSection, and that gives errors.

--EDIT--

If I add this instead of @RenderSection()

<div id="navigation">
          = @RenderPage("~Views/Shared/Navigation.cshtml")
        </div>

I get this:

The file "~/Views/Shared/~Views/Shared/Navigation.cshtml" could not be rendered, because it does not exist or is not a valid page.

--EDIT--

FULL _Layout.cshtml:

   <!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript">    </script>
</head>

<body>
<div id="wrapper">

    <div id="content">
        <!-- BANNER -->
        <div id="banner">

        </div>
        <!-- NAVIGATION -->
        <div id="navigation">
         @RenderPage("Navigation.cshtml")
        </div>
        <!-- MAIN DISPLAY -->
        <div id="main">
            @RenderBody()
        </div>
        <!-- FOOTER -->
        <div id="footer">

        </div>
    </div>
</div>

3条回答
成全新的幸福
2楼-- · 2019-08-03 23:06

Try with full path of view or you can use Html.Partial() or @Html.RenderPartial():

<div id="navigation">
                @RenderPage("Navigation.cshtml")
</div>

Html.Partial():

<div id="navigation">
            @Html.Partial("~/Views/Shared/Navigation.cshtml")
</div>

Html.RenderPartial():

<div id="navigation">
            @{ Html.RenderPartial("~/Views/Shared/Navigation.cshtml"); }
</div>
查看更多
甜甜的少女心
3楼-- · 2019-08-03 23:10

Try just to render a page "Views/Shared/Navigation.cshtml" as a partial view like this:

<div id="navigation">
    @Html.RenderPartial("Navigation")
</div>
查看更多
乱世女痞
4楼-- · 2019-08-03 23:19

If you want to keep the @RenderSection("Navigation") in your _Layout you can try below code in your view.

@section Navigation{
    @Html.Partial("~/Views/Shared/Navigation.cshtml")
}

OR you can change the _Layout as below.

<div id="navigation">
    @Html.Partial("~/Views/Shared/Navigation.cshtml")
</div>
查看更多
登录 后发表回答