Skip to content Skip to sidebar Skip to footer

Include Only Part Of A Partial View With Asp.net Razor Mvc

I am using ASP.NET Razor MVC and am using Partial Views for common content that I don't want to update on every single page. I am using the below syntax to include my partial vie

Solution 1:

You could make the partial strongly typed to a view model:

publicclassMyViewModel
{
    publicbool ShowOnlyPartA { get; set; }
}

and then make your view strongly typed to this model:

@modelMyViewModel

<div class="divA">
    CONTENT
</div>

@if (Model == null || !Model.ShowOnlyPartA)
{
    <divclass="divB">
        CONTENT
    </div>
}

and then you could call your partial like this:

@Html.Partial("PartialView", newMyViewModel { ShowOnlyPartA = true }) 

or like this:

@Html.Partial("PartialView") 

Solution 2:

Excellent question as well as an answer from Darin. As an alternative, pass a string instead:

<!-- View -->
@Html.Partial("PartialView", "divA") 

<!-- PartialView -->
@if (Model == "divA")
{
  <divclass="divA"></div>
}

@if (Model == "divB")
{
  <divclass="divB"></div>
}

Post a Comment for "Include Only Part Of A Partial View With Asp.net Razor Mvc"