部分视图中的ASP.NET MVC自定义类型列表

时间:2009-06-22 16:08:36

标签: asp.net-mvc partial-views

我有一个带有PaginatedList的视图(就像Nerd Dinner样本一样)。该页面按预期工作。现在我添加了一个部分视图,它采用相同的PaginatedList,并在第一个视图中调用RnederPartial。 ASP.NET抛出了一个我似乎无法解决的异常。

PaginatedList代码:

public class PaginatedList<T> : List<T>
{
    public int PageIndex { get; private set; }
    public int PageSize { get; private set; }
    public int TotalCount { get; private set; }
    public int TotalPages { get; private set; }

    public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
    {
        PageIndex = pageIndex;
        PageSize = pageSize;
        TotalCount = source.Count();
        TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
        this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
    }

    public bool HasPreviousPage
    {
        get
        {
            return (PageIndex > 0);
        }
    }

    public bool HasNextPage
    {
        get
        {
            return (PageIndex + 1 < TotalPages);
        }
    }
}

控制器:

[Authorize]
public ActionResult Index(int? page)
{
    const int pageSize = 10;

    var workstations = itilRepository.FindAllWorkstations();

    var paginatedWorkstations = new PaginatedList<Workstation>(workstations, page ?? 0, pageSize);

    return View("Index", paginatedWorkstations);
}

索引顶部视图:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ITILDatabase.Helpers.PaginatedList<ITILDatabase.Models.Workstation>>" %>

我在Index视图中调用部分视图,如下所示:

<% Html.RenderPartial("Workstations", Model); %>

工作站顶部部分视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewPage<ITILDatabase.Helpers.PaginatedList<ITILDatabase.Models.Workstation>>" %>

我收到的错误是:

c:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ Temporary ASP.NET Files \ root \ d2889d23 \ c6192b3e \ App_Web_workstations.ascx.a8d08dba.tgrd74s0.0.cs(156):错误CS0030:无法转换输入'ASP.views_home_workstations_ascx'到'System.Web.Mvc.ViewUserControl'

如果有人知道我为什么会收到这个错误,我会非常感激。

谢谢!

1 个答案:

答案 0 :(得分:24)

您的工作站部分视图的顶部应如下所示:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ITILDatabase.Helpers.PaginatedList<ITILDatabase.Models.Workstation>>" %>

将ViewPage更改为ViewUserControl