简体   繁体   English

根据Razor MVC4中的条件从控制器控制局部视图

[英]Controling Partial view from controller based on condition in Razor MVC4

In my home view I have below HTMl with 3 partial views. 在我的主视图中,我在HTM1下有3个局部视图。 In that First Partial view will render always. 在该“第一部分”视图中将始终呈现。 Second and Third will be decided by home controller based on Flag 0 or 1. How to handle such situation. 第二和第三将由家庭控制器根据标志0或1决定。如何处理这种情况。 Please suggest me. 请给我建议。

<div>             
    <div>
        -- some HTML
    </div>   
    @{ Html.RenderAction("First", "First");}
    @{ Html.RenderAction("Second", "Second");}
    @{ Html.RenderAction("Third", "Third");}

</div>

What's the issue if you decide you flag in your controller as you are saying and then sent it to view and then simply use this flag in the view to decide whether or not to render second and third? 如果您决定说出自己在控制器中的标记,然后将其发送到视图,然后在视图中简单地使用此标记来决定是否渲染第二个和第三个,这是什么问题?

In Controller 在控制器中

ViewBag.Flag = bool // 0 or 1 as per your logic

In View 视野中

if(ViewBag.Flag){
    @{ Html.RenderAction("Second", "Second");}
    @{ Html.RenderAction("Third", "Third");}
}

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

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