简体   繁体   中英

how to render partial view conditionally

i'm having a view (parent view) consisting of a child view (partial view) as below,

@{ Html.RenderPartial("_partialViewName",Model);}

@(Html.X().Panel().
        Items(Html.X().Container().ID("cntrGenerateReportResult")
          )
    )

is it possible to show/hide (Render) the partial view conditionally based on a property value from Model

i tried,

@if(Model.Type != "RER")
{ 
    Html.RenderPartial("_partialViewName", Model); 
}

but not getting the panel below rendered..

If Type has value 'RER' it should be rendered:

    @if(Model.Type.Trim() == "RER")
    { 

    }
    else
    {
     Html.RenderPartial("_partialViewName", Model); 
    }

you have to make sure that Type is not "RER" otherwise it should work

If your main view is bound with model & partial view also required a model then bind both of the model in partial view using "Tuple". Send partial view name through ViewBag to the view.

@if (@ViewBag.PartialView != null && @ViewBag.PartialView!= "")
{
    @Html.Partial(@ViewBag.PartialView)
}

You can use model for main view using Model.Item1/2/3/4 directly.

But disadvantage of this is that you have to bind a blank partial view when you don't need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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