Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert "File1.cshtml" into a section in main view

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>

like image 938
Ace Avatar asked Dec 20 '22 12:12

Ace


1 Answers

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>
like image 143
Ehsan Sajjad Avatar answered Jan 17 '23 20:01

Ehsan Sajjad