我的页面模型如下所示:
public class SomeViewModel()
{
public List<Some> Something1 {get;set;}
public MySettings Settings {get;set;}
}
现在在我看来:
@foreach(var some in Something1)
{
@Html.Partial("_Some", some);
}
如果我Settings
也想传入局部怎么办,我该怎么做?
我的页面模型如下所示:
public class SomeViewModel()
{
public List<Some> Something1 {get;set;}
public MySettings Settings {get;set;}
}
现在在我看来:
@foreach(var some in Something1)
{
@Html.Partial("_Some", some);
}
如果我Settings
也想传入局部怎么办,我该怎么做?
您可以创建另一个视图模型类型:
public class OtherViewModel()
{
public OtherViewModel(Some some, MySettings mySettings)
{
Some = some;
MySettings = mySettings;
}
public Some Some { get; set; }
public MySettings Settings { get; set; }
}
然后你可以使用它你的视图:
@foreach(var some in Something1)
{
@Html.Partial("_Some", new OtherViewModel(some, Model.MySettings));
}
您也可以在不创建类型的情况下使用Tuple类,但我认为第一个是更好的方法。无论如何,您可以这样做:
@foreach(var some in Something1)
{
@Html.Partial("_Some", new Tuple<Some,MySettings>(some, Model.MySettings));
}