我正在尝试在ASP.NET MVC应用程序中构建一个通用的网格视图。
让我用一些代码来解释:
public interface ITrustGrid<T>
{
IPagedList<T> Elements { get; set; }
IList<IColumn<T>> Columns { get; set; }
IList<string> Headers { get; }
}
这是一个类的接口,它允许我在我的控制器中设置列和表达式。
我像这样将实现传递给部分视图:
<% Html.RenderPartial("SimpleTrustGridViewer", ViewData["employeeGrid"] as TrustGrid<EmployeeInfoDTO>); %>
问题是我不知道如何使呈现网格的部分视图通用化。
换句话说,我想把这个:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ITrustGrid<EmployeeInfoDTO>>" %>
变成像这样的内容:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ITrustGrid<T>>" %>
如何以最简单的方式使我的局部视图变成通用视图?
编辑:
我通过使用一个具有公共的GetTrustGrid()方法的TrustGridBuilder解决了这个问题,该方法返回一个非泛型的TrustGrid。TrustGrid包含字符串而不是linq代码。因此,我在GetTrustGrid()方法中执行linq,并将字符串放入TrustGrid对象中。
感谢大家帮助我走上正确的道路。