多态视图模型收集和呈现在MVC局部视图 [英] Polymorphic ViewModel collection and rendering in MVC partial Views
问题描述
我在与我的MVC应用程序中的ViewModels的多态集合的问题。我通过一个Web服务调用收到此,我需要通过他们迭代,并给他们自己的局部视图,基于对象的类型。
I'm having a problem with a polymorphic collection of ViewModels in my MVC application. I received this via a web service call and i need to iterate through them and give them their own partial view, based on the object type.
public abstract class ProvinceViewModel
{
public string Code { get; set; }
}
public sealed class OntarioViewModel : ProvinceViewModel { }
public sealed class QuebecViewModel : ProvinceViewModel {}
在我看来,我试图通过它们进行迭代并分配的局部视图。我也做了很多类型转换的位置,使其工作。如果我尝试和移动这一个控制器动作,并通过在抽象类型,我会得到,我们不能创建抽象类的实例错误。
In my view i am trying to iterate through them and assign a partial view. I have to do a lot of type casting here to make it work. If I try and move this to a controller action and pass in the abstract type, i will get an error that we cannot create an instance of abstract class.
ICollection<ProvinceViewModel> ProvinceList; // collection receive via service
@for (int i = 0, c = ProvinceList.Count; i < c; i++)
{
var currentProvince = this.Model.ElementAt(i);
@switch (additionalRegistry.Code)
{
case "QC":
@Html.Partial("AlbertaDetail", (QuebecViewModel)currentProvince)
break;
case "ON":
@Html.Partial("OntarioDetail", (OntarioViewModel)currentProvince)
break;
default:
@Html.Partial("ProvinceDetail", ProvinceViewModel)
break;
}
}
我强类型视图,这样我可以访问不同的属性。
I have strongly type View, so that i can access the different properties.
我将如何去在一个更优雅的方式解决这个?我需要创建一个新的代理基类的抽象类来创建它的一个实例,更容易?
How would i go about solving this in a more elegant way? Would I need to create a new surrogate base class for the abstract class to create a instance of it easier?
推荐答案
一旦在过去遇到了同样的问题,我创建了以下解决方案:
Upon encountering the same problem in the past, I have created the following solution:
首先,装点您的(混凝土)视图模型使用表示要使用的视图名称 ExportMetadata code>属性。例如:
First, decorate your (concrete) view-model with ExportMetadata
attribute that denotes the view name to be used. For example:
[ExportMetadata("View", "Ontario")]
public sealed class OntarioViewModel : ProvinceViewModel { }
[ExportMetadata("View", "Quebec")]
public sealed class QuebecViewModel : ProvinceViewModel {}
那么你的的HtmlHelper
具有以下部分
方法扩展:
public static MvcHtmlString Partial<T>(this HtmlHelper htmlHelper, T model, string prefix = null)
{
var modelType = typeof (T);
var partialAttr = modelType.GetCustomAttributes<ExportMetadataAttribute>().SingleOrDefault(x => x.Name == "View");
if (partialAttr == null)
throw new Exception(modelType.Name + " doesn't define any view to be used");
var partialName = (prefix ?? String.Empty) + partialAttr.Value;
return htmlHelper.Partial(partialName, model, htmlHelper.ViewData);
}
然后使用它:
@Html.Partial(currentProvince);
和在情况下,你的谐音存在于某些子目录:
And in case your partials reside in some sub-directory:
@Html.Partial(currentProvince, "Partials/")
(如果您需要帮助注册自定义HTML辅助见 http://stackoverflow.com/a/5052790 )
这篇关于多态视图模型收集和呈现在MVC局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!