如何解决“ Html.PagedListPager()”错误

时间:2019-04-16 12:14:40

标签: javascript c# html asp.net-mvc visual-studio

您好,我目前正在一个需要分页的网站上工作。但是,我不断收到一条错误消息:

严重性代码描述项目文件行抑制状态 错误CS1929'IHtmlHelper>'不包含'PagedListPager'的定义,最佳扩展方法重载'HtmlHelper.PagedListPager(HtmlHelper,IPagedList,Func)'要求类型为'HtmlHelper'MVCManukauTech的接收器

我已经为PagedList,PagedList.Mvc和X.PagedList.Mvc.Core添加了using语句。

我的控制器:

  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 MVCManukauTech.Models.DB;
  using MVCManukauTech.ViewModels;
  using PagedList;
  using X.PagedList.Mvc.Core;
  using PagedList.Mvc;


  namespace MVCManukauTech.Controllers
{
public class CatalogController : Controller
{
    private readonly F191_Grace_ProjectContext _context;

    public CatalogController(F191_Grace_ProjectContext context)
    {
        _context = context;
    }




    public IActionResult Index(string searchBy, string search, int? page) 
    {
        //140903 JPC add CategoryName to SELECT list of fields
        string SQL = "SELECT ProductId, Product.CategoryId AS CategoryId, Name, ImageFileName, UnitCost"
            + ", SUBSTRING(Description, 1, 100) + '...' AS Description, CategoryName "
            + "FROM Product INNER JOIN Category ON Product.CategoryId = Category.CategoryId ";
        string categoryName = Request.Query["CategoryName"];

        if (categoryName != null)
        {
            //140903 JPC security check - if ProductId is dodgy then return bad request and log the fact 
            //  of a possible hacker attack.  Excessive length or containing possible control characters
            //  are cause for concern!  TODO move this into a separate reusable code method with more sophistication.
            if (categoryName.Length > 20 || categoryName.IndexOf("'") > -1 || categoryName.IndexOf("#") > -1)
            {
                //TODO Code to log this event and send alert email to admin
                return BadRequest(); // Http status code 400
            }

            //140903 JPC  Passed the above test so extend SQL
            //150807 JPC Security improvement @p0
            SQL += " WHERE CategoryName = @p0";
            //SQL += " WHERE CategoryName = '{0}'";
            //SQL = String.Format(SQL, CategoryName);
            //Send extra info to the view that this is the selected CategoryName
            ViewBag.CategoryName = categoryName;
        }

        //150807 JPC Security improvement implementation of @p0
        var products = _context.CatalogViewModel.FromSql(SQL, categoryName);
        return View(products.ToList().ToPagedList( page??1, 6));



    }

我的观点:

  @using PagedList.Mvc;
  @using X.PagedList.Mvc.Core;
  @using PagedList;
  @model IPagedList<MVCManukauTech.ViewModels.CatalogViewModel>
  @{

//Are we showing all the products or only one category?
if (ViewBag.CategoryName == null)
{
    ViewBag.Title = "Catalog";
}
else
{
    ViewBag.Title = "Catalog - " + ViewBag.CategoryName;
}
}
<link href="~/css/StyleSheet.css" rel="stylesheet" />

 <div class="bg">

<h2>@ViewBag.Title</h2>




<div class="text-center">
    <a href="~/Catalog"><button type="button" class="btn  btn-lg">All</button></a>
    <a href="~/Catalog?CategoryName=Transports"><button type="button" class="btn btn-lg">Transports</button></a>
    <a href="~/Catalog?CategoryName=Gadgets"><button type="button" class="btn  btn-lg">Gadgets</button></a>
    <a href="~/Catalog?CategoryName=Furnitures"><button type="button" class="btn  btn-lg">Furnitures</button></a>
    <a href="~/Catalog?CategoryName=Kitchen"><button type="button" class="btn  btn-lg">Kitchen</button></a>
    <a href="~/Catalog?CategoryName=Entertainment"><button type="button" class="btn  btn-lg">Entertainment</button></a>
    <a href="~/Catalog?CategoryName=Bathroom"><button type="button" class="btn  btn-lg">Bathroom</button></a>
    <a href="~/Catalog?CategoryName=Technology"><button type="button" class="btn  btn-lg"> Technology</button></a>
</div>








<table class="table" style="background-color:snow">
    <tr>

        <th>
            Name
        </th>

        <th>
            Image
        </th>
        <th>
            Unit Cost
        </th>
        <th>
            Description
        </th>
        <th>
            Category
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr class="d-block">

            <td>
                @item.Name
            </td>
            <td>
                <img src="~/Images/Product_Images/@item.ImageFileName" style="width:100px" />
            </td>
            <td style="text-align: right">
                @item.UnitCost
            </td>
            <td>
                @item.Description
            </td>
            <td>
                @item.CategoryName
            </td>
            <td>
                <a href="~/OrderDetails/ShoppingCart?ProductId=@item.ProductId"><button>Add&nbsp;to&nbsp;Cart</button></a>
            </td>
            <td>
                <a href="~/Catalog/Details?ProductId=@item.ProductId"><button>Details</button></a>
            </td>
        </tr>
        <tr></tr>
    }

</table>



   @Html.PagedListPager(Model, page => Url.Action("Index","Action", new { page }));

0 个答案:

没有答案