2012-12-05 19 views
2

我要创建共享局部视图Partial1,它会在其它屏幕来使用。 因此,而不是创建特定的模型PartialModel1的共享组件,可以创建界面IPartialModel1将接口用作共享组件的模型是否是一种很好的方法?

然后其他屏幕可以实现IPartialModel1 像

BigScreenModel1:IPartialModel1 
BigScreenModel2:IPartialModel1 

所以在BigScreen.cshtml我可以使用

@Html.Partial("Partial",BigScreenModel1) 

和BigScreen2.cshtml

@Html.Partial("Partial",BigScreenModel2) 

这是很好的做法?

+1

我的倾向是“否”。为什么你不想用相同的数据对象填充相同的部分时,从不同的视图调用它? – Bobson

+0

部分视图与数据无关。它包含的代码可以增强来自任何具有新功能的视图的数据网格。 –

回答

3

我有这种方法,虽然它会工作的问题,是你绑你的局部的视图模式的无所不包的页面。

一个更好的方法是将有局部视图模型作为网页中的一员查看模型,如下所示:

public class BigScreenModel1 
{ 
    public PartialViewModel OtherViewModel { get; set; } 
} 

public class BigScreenModel2 
{ 
    public PartialViewModel OtherViewModel { get; set; } 
} 

然后当然是:

@Html.Partial("Partial", Model.OtherViewModel) 

在两个页面。这使得视图模型彼此分离。

+0

是的,这就是我的想法,虽然你说的比我想的要好。这是做到这一点的正确方法。 – Bobson

相关问题