我在导航栏上创建了一个全局下拉过滤器。我收到错误:
处理请求时发生未处理的异常。
InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“System.String”,但此 ViewDataDictionary 实例需要类型为“Website.ViewModels.CardsViewModel.CardsViewModel”的模型项。
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(对象值)
这是我的代码-
FilterController.cs
:
FilterController.cs中的
_cache.Set()
将代码重定向到PostController.cs。只有这样我的代码才会出错。public IActionResult Index(string postCategory)
{
var categoryLst = new List<string>();
var categoryQry = from p in _context.Posts
orderby p.PostCategory
select p.PostCategory;
categoryLst.AddRange(categoryQry.Distinct());
ViewBag.movieGenre = new SelectList(categoryLst);
var posts = from c in _context.Posts
select c;
if (!string.IsNullOrEmpty(postCategory))
{
posts = posts.Where(x => x.PostCategory == postCategory);
}
_cache.Set("category", postCategory);
ViewBag.PostCategory = postCategory;
string url = Request.Headers["Referer"].ToString();
return Redirect(url);
}
PostController.cs
:
FilterController.cs中的
_cache.Set()
将代码重定向到此处。[HttpGet]
public async Task<IActionResult> Index()
{
var CardPostVM = new CardsViewModel
{
PostCard = await _context.Posts.ToListAsync()
};
var cached = _cache.TryGetValue("category", out var postCategory);
if (cached)
{
return View(postCategory);
}
return View(CardPostVM);
}
(参考文件):
使用 _PartialLayout
_TopNavbar.cshtml
作为我的导航栏。这是下拉过滤器的视图:
<!-- dropdown filter -->
<form class="ps-lg-5" asp-controller="Filter" asp-action="Index" id="category" method="get">
<div class="input-group ps-lg-5">
<select class="form-control shadow-none" name="postCategory" id="post-filter">
<option>All</option>">
@foreach (var item in cardsVM.PostCategories)
{
<!option value="@item.CategoryName" @(item.CategoryName.ToString() == ViewBag.PostCategory?.ToString() ? "selected" : "")>@item.CategoryName</!option>
}
</select>
<button class="btn btn-outline-success shadow-none me-2" type="submit"><i class='bx bx-filter'></i></button>
</div>
</form>
CardsViewModel.cs
(错误中提到):
public class CardsViewModel
{
public List<PostModel>? PostCard { get; set; }
}
更新(参考文件):
_Layout.cshtml
是提到_TopNavbar.cshtml
的地方。
<!DOCTYPE html>
<html lang="en">
<head>
<!--Links-->
</head>
<body id="body-pd">
<!--Top Nav bar-->
<div>
<partial name="~/Views/Shared/TopNavbar/_TopNavbar.cshtml" />
</div>
<!--Render Body-->
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<!--Links-->
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
这是一个 _PartialLayout 文件
_PostCards.cshtml
,其中包括 @model CardsViewModel:
@using RecipeWebsite.ViewModels.CardsViewModel
@model CardsViewModel
@foreach (var item in Model.PostCard)
{
// Detail of Card where my data is displayed
<div class="col mb-4">
<div class="card shadow-sm">
<img class="bd-placeholder-img card-img-top" src="@item.Image" width="157" height="236" alt="Card image cap">
<!--Card Details-->
<div class="card-body">
<a asp-controller="Post" asp-action="Detail" asp-route-id="@item.Id" class="stretched-link"></a>
<h6 class="card-text"> @item.Title </h6>
<div class="d-flex justify-content-between align-items-center mb-2">
<p class="small text-secondary">@item.PostCategory</p>
</div>
</div>
</div>
</div>
}
提及
PostCards.cshtml
的文件
@using RecipeWebsite.ViewModels.CardsViewModel
@model CardsViewModel
<main>
<!-- Cards -->
@if (Model.PostCard.Count != 0)
{
<div class="album py-5 bg-light">
<div class="container">
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
<partial name="~/Views/Shared/Cards/_PostCards.cshtml" />
</div>
</div>
</div>
}
else
{
// Error
}
</main>
谢谢你
FilterController.cs
和 PostController.cs
FilterController.cs
:
public async Task<IActionResult> Index(string postCategory)
{
var filteredPost = from p in _context.Posts select p;
if (!string.IsNullOrEmpty(postCategory))
{
ViewBag.PostCategory = postCategory;
filteredPost = filteredPost.Where(p => p.PostCategory == postCategory);
}
var filteredCategory = new CardsViewModel
{
PostCard = await filteredPost.ToListAsync()
};
_cache.Set("filteredCategory", filteredCategory);
string url = Request.Headers["Referer"].ToString();
return Redirect(url);
}
PostController.cs
[HttpGet]
public async Task<IActionResult> Index()
{
var CardPostVM = new CardsViewModel
{
PostCard = await _context.Posts.ToListAsync()
};
var cached = _cache.TryGetValue("filteredCategory", out var filteredCategory);
if (cached)
{
return View(filteredCategory);
}
return View(CardPostVM);
}