当我试图加载https://localhost:7242/product我得到以下错误
我不得不在这个页面中实现分页
InvalidOperationException:传入ViewDataDictionary的模型项的类型为“System.Linq.Enumerable+ d__2313[<>f__AnonymousType0
2[WebApplication3105.Models.Product,System. Collections. Generic. IEnumerable 1[WebApplication3105.Models.Category]],WebApplication3105.Models.Category,WebApplication3105.Models.ProductCategory]”,但此ViewDataDictionary示例需要类型为“X.PagedList.IPagedList
1[WebApplication3105.Models.ProductCategory]”的模型项。
模型类
namespace WebApplication3105.Models
{
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
}
namespace WebApplication3105.Models
{
public class Category
{
public int CategoryId { get; set; }
public String CategoryName { get; set; }
public ICollection<Product> Product { get; set; }
}
}
namespace WebApplication3105.Models
{
public class ProductCategory
{
public Product product { get; set; }
public Category category { get; set; }
}
}
控制器代码
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PagedList.Mvc;
using WebApplication3105.Data;
using WebApplication3105.Models;
using X.PagedList;
namespace WebApplication3105.Controllers
{
public class ProductController : Controller
{
private readonly MVCDbContext dbContext;
public ProductController(MVCDbContext dbContext)
{
this.dbContext = dbContext;
}
[HttpGet]
public async Task<IActionResult> Index(int? page)
{
List<Product> products = dbContext.Product.ToList();
List<Category> categories = dbContext.Category.ToList();
int recordsPerPage = 2;
var data = from p in products
join c in categories on p.CategoryId equals c.CategoryId into table1
from c in table1.ToList().ToPagedList(page ?? 1, recordsPerPage)
select new ProductCategory
{
product = p,
category = c
};
return View(data);
}
[HttpGet]
public IActionResult AddProduct()
{
return View();
}
[HttpPost]
public async Task<IActionResult> AddProduct(Product productRequest)
{
var product = new Product()
{
ProductName = productRequest.ProductName,
CategoryId = productRequest.CategoryId
};
await dbContext.Product.AddAsync(product);
await dbContext.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}
查看页面
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
@using X.PagedList.Mvc.Core;
@using X.PagedList
@model IPagedList<WebApplication3105.Models.ProductCategory>
<h1 class="text-center text-uppercase">
List of products
</h1>
<table class="table table-striped table-bordered">
<thead class="bg-dark text-white">
<tr>
<th>Product Id</th>
<th>Product Name</th>
<th>Category Id</th>
<th>Category Name</th>
</tr>
</thead>
<tbody>
@foreach (var data in Model)
{
<tr>
<th>@Html.DisplayFor(modelItem =>data.product.ProductId)</th>
<th>@Html.DisplayFor(modelItem =>data.product.ProductName)</th>
<th>@Html.DisplayFor(modelItem =>data.product.CategoryId)</th>
<th>@Html.DisplayFor(modelItem =>data.category.CategoryName)</th>
</tr>
}
</tbody>
</table>
<div>
<div class="pagedList">
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
</div>
</div>
1条答案
按热度按时间uplii1fm1#
测试结果
你应该像下面这样修改你的代码:
问题原因
数据源错误,您应该更改linq查询。