ASP.NET MVC PartialView通用ModelView

时间:2022-11-30 17:33:19

I have an ASP.NET MVC application which I want to dynamically pick the partial view and what data gets passed to it, while maintaining strong types.

我有一个ASP.NET MVC应用程序,我想动态选择局部视图和传递给它的数据,同时保持强类型。

So, in the main form, I want a class that has a view model that contains a generically typed property which should contain the data for the partial view's view model.

因此,在主窗体中,我想要一个具有视图模型的类,该视图模型包含一般类型的属性,该属性应包含局部视图的视图模型的数据。

public class MainViewModel<T>
{
    public T PartialViewsViewModel { get; set; }
}

In the User Control, I would like something like:

在用户控件中,我想要像:

Inherits="System.Web.Mvc.ViewUserControl<MainViewModel<ParticularViewModel>>" %>

Though in my parent form, I must put

虽然在我的父母形式,我必须把

Inherits="System.Web.Mvc.ViewPage<MainViewModel<ParticularViewModel>>" %>

for it to work.

它的工作原理。

Is there a way to work around this? The use case is to make the user control pluggable. I understand that I could inherit a base class, but that would put me back to having something like a dictionary instead of a typed view model.

有办法解决这个问题吗?用例是使用户控件可插拔。我知道我可以继承一个基类,但这会让我回到像字典而不是类型化的视图模型。

1 个解决方案

#1


1  

You can use the DisplayTemplates and EditorTemplates to accomplish this. So if I'm reading your question right, you have a setup like this:

您可以使用DisplayTemplates和EditorTemplates来完成此任务。因此,如果我正确地阅读您的问题,您可以使用以下设置:

If you are using .NET 4.0 (yay for covariant generics!)

如果你使用的是.NET 4.0(yay for covariant generics!)

System.Web.Mvc.ViewPage<MainViewModel<object>>

If you are using .NET 3.5:

如果您使用的是.NET 3.5:

System.Web.Mvc.ViewPage<MainViewModel<object>>

public class MainViewModel
{
    public object PartialViewsViewModel { get; set; }
}

You can then invoke DisplayFor on that object to get a partial view. So invoking:

然后,您可以在该对象上调用DisplayFor以获取部分视图。所以调用:

<%= Html.DisplayFor(m => m.PartialViewsViewModel) %>

Will look for a template in your DisplayTemplates folder for a skin of the name of your type. So if you have a ParticularViewModel.ascx in your DisplayTemplates, it will use that control as the 'partial view'. If you were using some other kind of view model type, then search for OtherViewModel.ascx (for example).

将在DisplayTemplates文件夹中查找您的类型名称的外观的模板。因此,如果您的DisplayTemplates中有一个ParticularViewModel.ascx,它将使用该控件作为“局部视图”。如果您使用的是其他类型的视图模型类型,则搜索OtherViewModel.ascx(例如)。

The template for ParticularViewModel.ascx would then have:

然后,ParticularViewModel.ascx的模板将具有:

System.Web.Mvc.ViewUserControl<ParticularViewModel>

Which lets you treat the object as a strongly typed model.

这使您可以将对象视为强类型模型。

#1


1  

You can use the DisplayTemplates and EditorTemplates to accomplish this. So if I'm reading your question right, you have a setup like this:

您可以使用DisplayTemplates和EditorTemplates来完成此任务。因此,如果我正确地阅读您的问题,您可以使用以下设置:

If you are using .NET 4.0 (yay for covariant generics!)

如果你使用的是.NET 4.0(yay for covariant generics!)

System.Web.Mvc.ViewPage<MainViewModel<object>>

If you are using .NET 3.5:

如果您使用的是.NET 3.5:

System.Web.Mvc.ViewPage<MainViewModel<object>>

public class MainViewModel
{
    public object PartialViewsViewModel { get; set; }
}

You can then invoke DisplayFor on that object to get a partial view. So invoking:

然后,您可以在该对象上调用DisplayFor以获取部分视图。所以调用:

<%= Html.DisplayFor(m => m.PartialViewsViewModel) %>

Will look for a template in your DisplayTemplates folder for a skin of the name of your type. So if you have a ParticularViewModel.ascx in your DisplayTemplates, it will use that control as the 'partial view'. If you were using some other kind of view model type, then search for OtherViewModel.ascx (for example).

将在DisplayTemplates文件夹中查找您的类型名称的外观的模板。因此,如果您的DisplayTemplates中有一个ParticularViewModel.ascx,它将使用该控件作为“局部视图”。如果您使用的是其他类型的视图模型类型,则搜索OtherViewModel.ascx(例如)。

The template for ParticularViewModel.ascx would then have:

然后,ParticularViewModel.ascx的模板将具有:

System.Web.Mvc.ViewUserControl<ParticularViewModel>

Which lets you treat the object as a strongly typed model.

这使您可以将对象视为强类型模型。