如何在主视图的部分中插入“ File1.cshtml”

Ace*_*Ace 3 asp.net-mvc razor asp.net-mvc-4

好的,即时通讯可能做错了...但是在我的主要_Layout中,我有以下内容:

<div id="navigation">
            @RenderSection("Navigation")
        </div>
Run Code Online (Sandbox Code Playgroud)

在我的Index.cshtml视图中指向此:

@section Navigation{
  <-- Need this to point to Views/Shared/Navigation.cshtml -->
}
Run Code Online (Sandbox Code Playgroud)

但是我不想在其中包含所有代码的大文件,因此我需要知道如何指向本节内部的名为“ Navigation.cshtml”的文件-基本上,所以我将所有节分开存放,独立文件。

我尝试只在_Layout中而不是@RenderSection中执行@RenderPage(“ Navigation.cshtml”),这会导致错误。

- 编辑 -

如果我添加它而不是@RenderSection()

<div id="navigation">
          = @RenderPage("~Views/Shared/Navigation.cshtml")
        </div>
Run Code Online (Sandbox Code Playgroud)

我得到这个:

The file "~/Views/Shared/~Views/Shared/Navigation.cshtml" could not be rendered, because it does not exist or is not a valid page.
Run Code Online (Sandbox Code Playgroud)

- 编辑 -

完整的_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>
Run Code Online (Sandbox Code Playgroud)

Ehs*_*jad 5

尝试使用完整的视角,或者可以使用Html.Partial()@Html.RenderPartial()

<div id="navigation">
                @RenderPage("Navigation.cshtml")
</div>
Run Code Online (Sandbox Code Playgroud)

Html.Partial():

<div id="navigation">
            @Html.Partial("~/Views/Shared/Navigation.cshtml")
</div>
Run Code Online (Sandbox Code Playgroud)

Html.RenderPartial():

<div id="navigation">
            @{ Html.RenderPartial("~/Views/Shared/Navigation.cshtml"); }
</div>
Run Code Online (Sandbox Code Playgroud)


Sar*_*nga 5

如果您想保留@RenderSection("Navigation")在您的视图中,_Layout您可以在您的视图中尝试以下代码。

@section Navigation{
    @Html.Partial("~/Views/Shared/Navigation.cshtml")
}
Run Code Online (Sandbox Code Playgroud)

或者您可以更改_Layout如下。

<div id="navigation">
    @Html.Partial("~/Views/Shared/Navigation.cshtml")
</div>
Run Code Online (Sandbox Code Playgroud)