The type 'IEnumerable<>' is defined in an assembly that is not referenced
Asked Answered
R

2

24

I have added the following nuget package to my MVC 5 application X.PagedList.Mvc

I return the results in my controller/view as follows:

// 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>

But in my view I am getting the following error The type 'IEnumerable<>' is defined in an assembly that is not referenced. System.Runtime...

I have no project.json file in my application so what is this error?

Ranchero answered 8/7, 2016 at 19:12 Comment(4)
Could you show using statements?Braswell
Make sure you don't have a reference to System.Runtime. That can make things screwyAlibi
Are you referencing and PCLs such as Microsoft BCL Portability Pack? Ensure all of your assembly versions are the same, delete your .suo and do a clean/build.Sprague
need solution with this issuePipe
O
25

Make sure you have the following lines in your Web.config file:

<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>
Och answered 19/8, 2016 at 13:3 Comment(2)
For future reference, I had to close my Visual Studio and then reopen Visual Studio. The errors did not go away, even after following this answer, until I closed and reopened.Crocus
It seems XPagedList needs these lines in order to get rid off warnings about Model in cshtml files. I also deleted bin and obj folders in all projects, closed Visual Studio 2017 and reopened the solution. Now build is working with no errors.Joann
L
17

I know its a bit "too" late for answering, but for who just had this error I solved it just as the second half of the error says which is just adding an assembly reference for net standard in the assemblies section in the web config file and as following:

<configuration>
  ...
  <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>
Lysimeter answered 16/2, 2019 at 18:17 Comment(1)
This is the only solution that worked for me. Thank you! And, you're right. This is exactly what the error message indicated I needed to do to resolve the error in Visual Studio 2017.Saracen

© 2022 - 2024 — McMap. All rights reserved.