“IEnumerable <>”类型在未引用的程序集中定义

问题描述 投票:17回答:2

我已将以下nuget包添加到我的MVC 5应用程序X.PagedList.Mvc中

我在控制器/视图中返回结果如下:

// Repo
public IPagedList<Post> GetPagedPosts(int pageNumber, int pageSize)
{
   var posts = _context.Post
      .Include(x => x.Category)
      .Include(x => x.Type);

   // Return a paged list
   return posts.ToPagedList(pageNumber, pageSize);

}

// View model
public class PostViewModel
{
   public IPagedList<Post> Posts { get; set; }
   ...
}

// Controller method
public ActionResult Index(int? page)
{

    int pageNumber = page ?? 1;
    int pagesize = 5;

    var posts = _PostRepository.GetPagedPosts(pageNumber, pagesize);

    var viewModel = new PostViewModel
    {
        Posts = posts,
        ...
    };

    return View(viewModel);
}

// View
@model MyApp.ViewModels.PostViewModel
@using X.PagedList.Mvc;
@using X.PagedList;

<p>Page @(Model.Posts.PageCount < Model.Posts.PageNumber ? 0 : Model.Posts.PageNumber) of @Model.Posts.PageCount </p>

但在我看来,我得到以下错误The type 'IEnumerable<>' is defined in an assembly that is not referenced. System.Runtime...

我的应用程序中没有project.json文件,这是什么错误?

c# .net asp.net-mvc pagedlist
2个回答
20
投票

确保Web.config文件中包含以下行:

<compilation debug="true" targetFramework="4.6.1"> //don't need to change THIS line, just the content of this section
  <assemblies>
    <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    <add assembly="System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </assemblies>
</compilation>

1
投票

我知道它有点“太迟”回答,但是对于刚刚遇到这个错误的人我解决了它就像错误的后半部分说的那样只是在web配置文件的asemblies部分中添加了net standard的汇编参考如下:

<配置> ... <system.web> <compilation debug =“true”targetFramework =“4.6.1”> <assemblies> <add assembly =“netstandard,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = cc7b13ffcd2ddd51”/> </ assemblies> </ compilation> </system.web> ... </ configuration> ^ _ ^

© www.soinside.com 2019 - 2024. All rights reserved.