3

我有这个 _Layout.cshtml

<body>
    <div id="wrapper">
<div id="header-wrapper">
    <div id="header">
        <div id="logo">
                //SOME LOGO LOGIC

        </div>
    </div>
    <div id="menu-wrapper">

        //SOME MENU LOGIC

    </div>
    <!-- end #menu -->
    <div id="banner">
        <div id="slider"> <a href="#" class="button previous-button">&lt;</a> <a href="#" class="button next-button">&gt;</a>
            <div class="viewer">
                <div class="reel">
                    <div class="slide">
                        <h2>Stallen..</h2>
                        <p>Her kan du skrive noe tekst</p>
                        <a class="link" href="~/horse/riversidestar">Full story ...</a><img src="~/images/img01.jpg" width="900" height="350" alt="" /></div>
                    <div class="slide">
                        <h2>Riverside Star i naturen</h2>
                        <p>Her kan du skrive noe tekst</p>
                        <a class="link" href="~/horse/riversidestar">Full story ...</a><img src="~/images/img02.jpg" width="900" height="350" alt="" /></div>
                </div>
            </div>
            <div class="indicator">
                <ul>
                    <li class="active">1</li>
                    <li>2</li>
                </ul>
            </div>
        </div>
        <script type="text/javascript">//SOME SCRIPT</script>

    </div>
</div>

<div id="page">
    <div id="content">
         @RenderBody()

    </div>

    <div id="sidebar-bg">
        <div id="sidebar">
            <h2>Nyheter</h2>
            @{Html.RenderAction("ShortNewsList", "News");}
        </div>
    </div>

    <div style="clear: both;">&nbsp;</div>
</div>

现在,它只有在每个视图上都不同的 renderBody 部分。当我进入不同的视图时,如何使用 id=banner 更改 DIV 的内容

当我尝试将它添加到另一个视图中时,它当然会在布局中非常错误

4

1 回答 1

8

您可以使用Razor 部分。在您的布局中定义一个部分:

@RenderSection("Banner", required: false)

并在您看来覆盖此部分:

@section Banner {
    ... contents of the banner specific to that view
}

您还可以定义一个默认横幅,这样您就不需要在每个视图中覆盖:

@if (!IsSectionDefined("Banner"))
{
    ... put the default banner contents here
}
else
{
    @RenderSection("Banner", required: true)
}
于 2012-06-13T06:43:26.107 回答