I'm trying to get a Core 6 project to paginate. I'm using code from an earlier .net tutorial and it's throwing all sort's of errors (below).
Any direction on how to update code in the Controller action method and Index view so that pagination works would be greatly appreciated.
Error(s)
.
Severity Code Description Project File Line Suppression State
Error (active) RZ1034 Found a malformed 'form' tag helper. Tag helpers must have a start and end tag or be self closing. C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Views\Model\Index.cshtml 19
Error CS0101 The namespace 'Project.Controllers' already contains a definition for 'ModelController' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 13 Active
Error CS0111 Type 'ModelController' already defines a member called 'ModelController' with the same parameter types Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 17 Active
Error CS0111 Type 'ModelController' already defines a member called 'Details' with the same parameter types Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 92 Active
Error CS0111 Type 'ModelController' already defines a member called 'Create' with the same parameter types Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 110 Active
Error CS0111 Type 'ModelController' already defines a member called 'Create' with the same parameter types Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 120 Active
Error CS0111 Type 'ModelController' already defines a member called 'Edit' with the same parameter types Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 132 Active
Error CS0111 Type 'ModelController' already defines a member called 'Edit' with the same parameter types Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 152 Active
Error CS0111 Type 'ModelController' already defines a member called 'Delete' with the same parameter types Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 183 Active
Error CS0111 Type 'ModelController' already defines a member called 'DeleteConfirmed' with the same parameter types Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 203 Active
Error CS0111 Type 'ModelController' already defines a member called 'ModelExists' with the same parameter types Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project_ExistingItem\ModelController.cs 219 Active
Error (active) CS0012 The type 'MvcHtmlString' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Views\Model\Index.cshtml 76
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 20 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 42 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 157 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 162 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 187 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 188 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 197 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 202 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 226 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 227 Active
Error CS0121 The call is ambiguous between the following methods or properties: 'ModelController.ModelExists(int)' and 'ModelController.ModelExists(int)' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 231 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 248 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 253 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 268 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 272 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 275 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 278 Active
Error CS0229 Ambiguity between 'ModelController._context' and 'ModelController._context' Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 284 Active
Warning CS0114 'ProjectContext.Model' hides inherited member 'DbContext.Model'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Data\ProjectContext.cs 17 Active
Warning CS8618 Non-nullable field '_context' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 18 Active
Warning CS8618 Non-nullable field '_context' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Controllers\ModelController.cs 18 Active
Warning NU1701 Package 'PagedList 1.17.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net6.0'. This package may not be fully compatible with your project. Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Project.csproj 1
Warning NU1701 Package 'PagedList.Mvc 4.5.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net6.0'. This package may not be fully compatible with your project. Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Project.csproj 1
Error RZ1034 Found a malformed 'form' tag helper. Tag helpers must have a start and end tag or be self closing. Project C:\Users\User1\Desktop\Solution\00 Solution\Solution\Project\Views\Model\Index.cshtml 19
.
.
.
.
.
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Project.Data;
using Project.Models;
using PagedList;
namespace Project.Controllers
{
public class ModelController : Controller
{
private readonly ProjectContext _context;
public ModelController(ProjectContext context)
{
_context = context;
}
public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.text = String.IsNullOrEmpty(sortOrder) ? "text_desc" : "";
ViewBag.number = sortOrder == "number" ? "number_desc" : "number";
// ViewBag.contactschooldistrict = sortOrder == "contactschooldistrict" ? "contactschooldistrict_desc" : "contactschooldistrict";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var models = from s in _context.Model
//where s.MediaPrivate != true
// && s.MediaPartner == true
select s;
if (!String.IsNullOrEmpty(searchString))
{
models = models.Where(s => s.Text!.Contains(searchString)
// || s.Number.Contains(searchString)
// || s.Production.Contains(searchString)
// || s.DateCreated.Contains(searchString) - needs other code to work
);
}
switch (sortOrder)
{
default:
models = models.OrderBy(s => s.Text).ThenBy(s => s.Number);
break;
case "text_desc":
models = models.OrderByDescending(s => s.Text);
break;
case "number":
models = models.OrderBy(s => s.Number);
break;
case "number_desc":
models = models.OrderByDescending(s => s.Number);
break;
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(models.ToPagedList(pageNumber, pageSize));
}
_________________________________________________________
Index
@model PagedList.IPagedList<Project.Models.Model>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Model";
}
<h1>Model</h1>
<p>
<a asp-action="Create">Create New</a>
@*@Html.ActionLink("Create New", "Create")*@
</p>
<form asp-controller="Model" asp-action="Index" method="get">
@*@using (Html.BeginForm("Index", "Model", FormMethod.Get))*@
<p>
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.ActionLink("Text", "Index", new { sortOrder=ViewBag.text, searchString=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Number", "Index", new { sortOrder=ViewBag.number, searchString=ViewBag.CurrentFilter })
</th>
@* <th>
@Html.DisplayNameFor(model => model.Bool)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>*@
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Text)
</td>
<td>
@Html.DisplayFor(modelItem => item.Number)
</td>
@* <td>
@Html.DisplayFor(modelItem => item.Bool)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>*@
<td>
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))