title: Fetching a model/object to use in a partialview in .Net Core
body: <p>I'm writing a .Net Core application and I have a pretty good grip on it after starting about 3 weeks ago.</p>
  <p>I have access to an online database using the Entity Framework (EF). I have 4 controllers controlling 4 different type of view categories (each category containing 5 views (index, delete, create, edit and details)). Each of the controllers are creating a PaginatedList in their index method holding either of the 4 model types that I have in my program. This is used to sort the items in the list based on different properties.</p>
  <h1>PaginatedList.cs</h1>
  <pre><code>public class PaginatedList&lt;T&gt; : List&lt;T&gt;
  {
      public int PageIndex { get; private set; }
      public int TotalPages { get; private set; }
  
      public PaginatedList(List&lt;T&gt; items, int count, int pageIndex, int pageSize)
      {
          PageIndex = pageIndex;
          TotalPages = (int)Math.Ceiling(count / (double)pageSize);
  
          this.AddRange(items);
      }
  
      public bool HasPreviousPage
      {
          get
          {
              return (PageIndex &gt; 1);
          }
      }
  
      public bool HasNextPage
      {
          get
          {
              return (PageIndex &lt; TotalPages);
          }
      }
  
      public static async Task&lt;PaginatedList&lt;T&gt;&gt; CreateAsync(IQueryable&lt;T&gt; source, int pageIndex, int pageSize)
      {
          var count = await source.CountAsync();
          var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
          return new PaginatedList&lt;T&gt;(items, count, pageIndex, pageSize);
      }
  }
  </code></pre>
  <p>The idea about the PaginatedList is that my database will grow in time and I want to limit how many items that are shown.</p>
  <p>I have implemented a small piece of code that marks a selected item in my table with the class &quot;success&quot;. Once this happens I want to grab the item that was &quot;selected&quot; and pass that to a PartialView so that I can show its properties, but due to using the PaginatedList as the model I am having a hard time accessing the item I want. I was hoping someone in here might have a solution to this?</p>
  <p>The following part is where I try to pass my user to my PartialView:</p>
  <h1>UserController index method</h1>
  <pre><code>public async Task&lt;IActionResult&gt; Index(
          int? page,
          int? id)
  {
  
      ....
  
      if (id != null)
      {
          ViewData[&quot;UserID&quot;] = id.Value;
      } 
  
      ....
  
      var users = from u in _context.Users select u;
  
      return View(await PaginatedList&lt;User&gt;.CreateAsync(users.AsNoTracking(), page ?? 1, pageSize));
  }
  </code></pre>
  <h1>User Index.cshtml</h1>
  <pre><code>@model PaginatedList&lt;Project.Models.User&gt;
  
  ....
  
  @if (itemSelected) //true if item is selected - false otherwise.
  {
      int elementID = (int)ViewData[&quot;ElementID&quot;];
      &lt;h4&gt;@elementID&lt;/h4&gt; //this return the id of the item that was selected
  
      //var user = Model.ElementAt&lt;User&gt;(elementID); //This is causing me trouble.
  
      @Html.Partial(&quot;PartialUserDetails&quot;, user)
  }
  </code></pre>
  <p>The PaginatedList is the &quot;Model&quot; in the above piece of code, as previously mentioned, so either I have to add my DbContext to that (don't know if possible) and using the <code>elementID</code> I should fetch the user or I have to come up with a different way of fetching users. The PaginatedList is though able to sort my items based on different criteria, so the items don't appear in an cronological fashion. E.g.</p>
  <pre><code>1, 2, 3, 4, 5
  </code></pre>
  <p>but rather</p>
  <pre><code>2, 1, 5, 4, 3
  </code></pre>
  <p>I was hoping someone in here might have a solution to my problem.</p>