简体   繁体   English

如何将参数从特定视图传递到 _Layout.cshtml?

[英]How to pass parameters from specific view to _Layout.cshtml?

I have a tool site, and the site layout was arranged like below.我有一个工具站点,站点布局如下所示。 Black square has the main content, and when you go to a specific tool now I want to display the related tools on the right side.黑色方块有主要内容,当你 go 到一个特定的工具时,我想在右侧显示相关的工具。

在此处输入图像描述

Now to avoid Col-md duplication on every page I arranged the Layout page like this现在为了避免在每个页面上出现 Col-md 重复,我这样安排了布局页面

  <div class="container">
        <div class="row">
            <div class="col-md-7">
                <main role="main" class="pb-3">
                    @RenderBody()
                </main>
            </div>
            <div class="col-md-5">
                //Related tools widget
            </div>
        </div>

So inside above code the I want to display this related tools widget.所以在上面的代码中,我想显示这个相关的工具小部件。 But there is a condition.但是有一个条件。 When you go to a specific tool, that specific tool should not be displayed in that widget.当您 go 到特定工具时,该特定工具不应显示在该小部件中。 So I created a partial view, and now I don't know where to call it.所以我创建了一个局部视图,现在我不知道在哪里调用它。

How to achieve that?如何实现? Is my structure all right?我的结构没问题吗? Instead of using col-md on the layout shall I use them on a specific view like the one below?除了在布局上使用 col-md 之外,我应该在如下所示的特定视图上使用它们吗?

  <div class="row">
            <div class="col-md-7">
               //Content goes here
            </div>
            <div class="col-md-5">
               @RenderSection("RelatedTools", required:true)
            </div>
        </div>

Since you have using @RenderSection("RelatedTools", required:true) ,you need to add @section RelatedTools {} in your each view:由于您使用@RenderSection("RelatedTools", required:true) ,因此您需要在每个视图中添加@section RelatedTools {}

Layout:布局:

<div class="container">
        <div class="row">
            <div class="col-md-7">
                <main role="main" class="pb-3">
                    @RenderBody()
                </main>
            </div>
            <div class="col-md-5">
                @RenderSection("RelatedTools", required:true)
            </div>
        </div>
       
    </div>
 

View:看法:

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

@section RelatedTools {
    <div><h1>RelatedTools</h1></div>
}

So that the html in @section RelatedTools will in the right side,and the html outside @section RelatedTools will in the left side.这样@section RelatedTools里面的html会在右边,@section @section RelatedTools外面的html会在左边。

Result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM