一般局部视图:如何设置一个泛型类的模式? [英] Generic partial view: how to set a generic class as model?
问题描述
我想建立一个ASP.NET MVC应用程序的通用网格视图。
I'm trying to build a generic grid view in an ASP.NET MVC application.
让我做一些code解释:
Let me explain with some code:
public interface ITrustGrid<T>
{
IPagedList<T> Elements { get; set; }
IList<IColumn<T>> Columns { get; set; }
IList<string> Headers { get; }
}
这是一类,让我一组列和前pressions在我的控制器的接口。
This is an interface of a class that allows me to set columns and expressions in my controller.
我通过实现,以这样的局部视图:
I pass implementations to a partial view like this:
<% Html.RenderPartial("SimpleTrustGridViewer", ViewData["employeeGrid"] as TrustGrid<EmployeeInfoDTO>); %>
问题是,我无法弄清楚如何使呈现电网通用的局部视图。
The problem is that I can't figure out how to make the partial view that renders the grid generic.
在换句话说,我希望把这个:
In other words, I want to turn this:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ITrustGrid<EmployeeInfoDTO>>" %>
弄成这个样子:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ITrustGrid<T>>" %>
=>我怎样才能让我的部分观点以最简单的方式通用的?
=> How can I make my partial view generic in the most simple way?
编辑:
我通过使用具有它返回一个非通用TrustGrid公共TrustGrid GetTrustGrid()方法TrustGridBuilder解决了这个。该TrustGrid包含字符串而非LINQ的东西。所以,我执行了GetTrustGrid()方法中的LINQ,并把串在TrustGrid对象。
I solved this by using a TrustGridBuilder that has a public TrustGrid GetTrustGrid() method which returns a non-generic TrustGrid. The TrustGrid contains strings instead of linq stuff. So I execute the linq in the GetTrustGrid() method and put the strings in a TrustGrid object.
感谢大家帮助我在正确的轨道上。
Thanks for everybody to help me on the right track.
推荐答案
这是不可能做到这一点这样的。原因是的.aspx
将生成你没有上太多的控制的一类,你不能一个泛型参数添加到它。我想最简单的方法就是把它作为对象
。
It's not possible to do it like that. The reason is the .aspx
will generate a class that you don't have much control on it and you can't add a generic parameter to it. I guess the most straightforward way is to pass it as object
.
这篇关于一般局部视图:如何设置一个泛型类的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!