简体   繁体   English

在MVC4中加载多个视图

[英]Loading Multiple Views In MVC4

I have just switched from PHP CI(MVC) to ASP.NET MVC4 and also i am new to .net framework. 我刚刚从PHP CI(MVC)切换到ASP.NET MVC4,而且我还是.net框架的新手。 I researched loading multiple views in MVC4 but i always found loading view with multiple Models and such.... What I am trying to do is load multiple view in a single page. 我研究了在MVC4中加载多个视图,但是我总是发现加载具有多个模型的视图,等等。...我想做的是在单个页面中加载多个视图。

For eg: 例如:

I have a view left.cshtml 我有一个left.cshtml视图

<div>Hello im on the left section</div>
@RenderBody();

and have next view main.cshtml 并具有下一个视图main.cshtml

<div class="left"> // should come in the left section of the page
    @{Layout = "left.cshtml"}
</div>
<div class="right">
    Contents at the right and many more here
</div>

and from above what i expected is: 从我所期望的是:

// Left section                     Right section

Hello im on the left section |  Contents at the right
                             |  and many more here
                             |
                             |
                             |
                             |

But The result was 但是结果是

Hello im on the left section
Contests at the right and many more here

and the CSS are also very ok. 而且CSS也很好。 Please help.... 请帮忙....

I think you are looking to render partial view. 我认为您正在寻找呈现部分视图的方法。 So use 所以用

@Html.Partial("ViewName")

OR 要么

@{ Html.RenderPartial("ViewName");  }

Layout is your main page, it goes "Around" of what is rendered via RenderBody . Layout是您的主页,它围绕通过RenderBody呈现的内容进行“围绕”。 So, if you want to have your right section always same, and substitute different views to your left section you can create a layout containing "structure" of the page, and then render different views into it. 因此,如果要使右部分始终相同,并用不同的视图替换左部分,则可以创建一个包含页面“结构”的布局,然后在其中呈现不同的视图。 Your code should be: 您的代码应为:

layout.chtml: layout.chtml:

<div class="left"> // should come in the left section of the page
 //Your left.chtml will be substituted here
   @RenderBody();
</div>
<div class="right">
    Contents at the right, they are always the same
</div>

left.chtml left.chtml

//This line shows which layout you are going to use
@{Layout = "left.cshtml"}
//Whole code below is substitued in plase of "RenderBody" of your layout page
Contents at the left. 

PS you have to return the left view, not the laout view in your contoller. PS,您必须返回左视图,而不是控制器中的布局视图。

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

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