仅包含与ASP.NET MVC剃刀的局部视图的一部分 [英] Include only part of a partial view with ASP.NET Razor MVC
问题描述
我使用ASP.NET 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 views:
@Html.Partial("PartialView")
在一个特定的部分观点,我有两个div:
On a particular partial view, I have two DIVS:
<div class="divA">
CONTENT
</div>
<div class="divB">
CONTENT
</div>
不过,我只想包括 DIVA
的内容。我可以这样做以下只包括 DIVA
?
However, I only want to include the content from divA
. Can I do something like the following to only include the content from divA
?
@Html.Partial("PartialView", @divA)
如果不是,我怎么可以这样做?
If not, how can I do so?
推荐答案
您可以使局部的强类型到视图模型:
You could make the partial strongly typed to a view model:
public class MyViewModel
{
public bool ShowOnlyPartA { get; set; }
}
,然后让你的视图强类型这一模式:
and then make your view strongly typed to this model:
@model MyViewModel
<div class="divA">
CONTENT
</div>
@if (Model == null || !Model.ShowOnlyPartA)
{
<div class="divB">
CONTENT
</div>
}
然后你可以打电话给你的部分是这样的:
and then you could call your partial like this:
@Html.Partial("PartialView", new MyViewModel { ShowOnlyPartA = true })
或者是这样的:
@Html.Partial("PartialView")
这篇关于仅包含与ASP.NET MVC剃刀的局部视图的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!