c# - Html Helper 'Action' 未定义,Asp.NET Core 2.2
问题描述
我是 ASP.NET Core MVC 的新手。我会先说我已经阅读了其他类似的问题,但它们与我遇到的问题并不完全相同。
我正在尝试实现动态左侧导航,其中每个按钮都是从我的数据库中加载的。
当我调用@Html.Action("NavMenu", "Nav")
“_Layout.cshtml”时出现错误
错误 CS1929
“IHtmlHelper”不包含“Action”的定义,最佳扩展方法重载“UrlHelperExtensions.Action(IUrlHelper, string, object)”需要“IUrlHelper”类型的接收器
其他类似的问题之一导致我添加@using System.Web.Mvc.Html
到我的“_ViewImports”的顶部,这导致了错误:
CS7069:对类型“RouteValueDictionary”的引用声称它在“System.Web”中定义,但找不到
还说@html.Action()
应该内置到Core 2.0中的答案
using@await Html.PartialAsync("_SideNav")
仅适用于我的索引视图,因为该视图具有@model IEnumerable<HSCWebApp.Models.HSCTable>
但我的其他视图需要@model HSCWebApp.Models.HSCTable
,这会导致“_SideNav”崩溃
我对其他方式来实现这一点100% 持开放态度。
在 NuGet 我已经安装:
- 实体框架
- Microsoft.AspNet.Mvc
- Microsoft.AspNetCore.App
- Microsoft.NETCore.App
编辑:
在构建过程中我也收到了这些警告:
警告 NU1701 包“Microsoft.AspNet.Mvc 5.2.2”是使用“.NETFramework,Version=v4.6.1”而不是项目目标框架“.NETCoreApp,Version=v2.1”恢复的。此软件包可能与您的项目不完全兼容。
以及用于实体框架的 AspNet.Razor AspNet.Webpages
这些警告会以某种方式影响我的引用吗?
以下是文件:
控制器:(“NavController.cs”)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using HSCWebApp.Models;
using Microsoft.EntityFrameworkCore;
namespace HSCWebApp.Controllers
{
public class NavController : Controller
{
private readonly HSCWebAppContext _context;
public NavController(HSCWebAppContext context)
{
_context = context;
}
public ActionResult NavMenu()
{
NavViewModel nav = new NavViewModel();
List<NavViewModel> navList = new List<NavViewModel>();
var items = from n in _context.HSCTable select n;
List<HSCTable> tableList = items.ToList<HSCTable>();
for (int i = 0; i < tableList.Count(); i++)
{
nav.Id = tableList[i].ID;
nav.DepartmentName = tableList[i].DepartmentName;
nav.sel = tableList[i].sel;
navList.Add(nav);
}
//var movies = from m in _context.Movie select m;
//IEnumerable<NavViewModel> en = navList;
//IEnumerable<HSCTable> en = tableList;
return PartialView("_SideNav", tableList);
}
}
}
型号:(“NavViewModel.cs”)
using System.ComponentModel.DataAnnotations.Schema;
namespace HSCWebApp.Models
{
[Table("HSCTable")]
public class NavViewModel
{
public NavViewModel(){
int Id = this.Id;
string DepartmentName = this.DepartmentName;
string sel = this.sel;
}
public int Id { get; set; }
public string DepartmentName { get; set; }
public string sel { get; set; }
}
}
查看:(“_SideNav.cshtml”)
@model IEnumerable<HSCWebApp.Models.HSCTable>
@using System;
@using System.Collections;
@using System.Linq;
@{
List<List<string>> navButtons = new List<List<string>>();
int listIndex = 0;
int INITIAL = 0;
navButtons.Add(new List<string>());
navButtons[listIndex].Add("");
}
<body>
<div class="sidenav">
@{
foreach (var item in Model)
{
if (navButtons[listIndex][INITIAL].Equals(null)
|| navButtons[listIndex][INITIAL].Equals(""))
{
// if the list of lists is empty, add the department name
//initialize the list with with the department name
navButtons[listIndex][INITIAL] = item.DepartmentName;
//add the current select
navButtons[listIndex].Add(item.sel);
}
else if (navButtons[listIndex][INITIAL].Equals(item.DepartmentName))
{
// if the current item's department name is the same
// is the current list's department name,
// add it's selectn
navButtons[listIndex].Add(item.sel);
}
else if (!item.DepartmentName.Equals(navButtons[listIndex][INITIAL]))
{
// if the current item's department name
// is not the same as the initial of this list,
// create new list
navButtons.Add(new List<string>());
listIndex++;
navButtons[listIndex].Add(item.DepartmentName);
navButtons[listIndex].Add(item.sel);
}
}
for (int i = 0; i < navButtons.Count; i++)
{
<button class="accordion"
onclick="openAccordion(this)">
@navButtons[i][INITIAL]
</button>
<div class="tab">
@for (int j = 1; j < navButtons[i].Count; j++)
{
<a class="tabLinks NoPrint"
onclick="openTab(this)"
asp-action="Details"
asp-route-id="@navButtons[i][j]">
@navButtons[i][j].ToString()
</a>
}
</div>
}
// @Url.Action("Details", "HSCTables", new { id = 1 })
//onclick = "location.href='@Url.Action("Details","HSCTables", new { id = 1 })
}
</div>
</body>
非常感谢您的宝贵时间。
解决方案
正如@Tiago Ávila 所说,Html.Action
它已从 Asp.NET Core 中删除,所以我不再尝试@html.action
使用 View Components 而是使用 View Components,正如 @Llazar 所建议的那样,它奏效了!
我的视图组件:
public class NavViewComponent : ViewComponent
{
private readonly HSCWebAppContext _context;
public NavViewComponent(HSCWebAppContext context)
{
_context = context;
}
public IViewComponentResult Invoke()
{
NavViewModel nav = new NavViewModel();
List<NavViewModel> navList = new List<NavViewModel>();
var items = from n in _context.HSCTable select n;
List<HSCTable> tableList = items.ToList<HSCTable>();
for (int i = 0; i < tableList.Count(); i++)
{
nav.Id = tableList[i].ID;
nav.DepartmentName = tableList[i].DepartmentName;
nav.sel = tableList[i].sel;
navList.Add(nav);
}
return View("_SideNav",tableList);
}
}
我在 _Layout 中的调用:
@await Component.InvokeAsync("Nav")
没有其他东西需要改变。谢谢你的帮助!
推荐阅读
- bootstrap-4 - 移动分辨率上的 boostrap 4 img-fluid strechs 图像
- logging - journalctl 单元记录在单元仍在运行时停止
- haskell - 在 Haskell 中递归搜索与名称条件匹配的所有文件的目录
- javascript - 你能在内容脚本中杀死监听底层页面事件的事件监听器吗?
- java - What is the SuppressLint Java annotation?
- azure-devops - VSTS 扩展——几个问题(数据存储和 React)
- matlab - 假设解的函数形式的最小二乘
- laravel - 如何通过 Laravel 中的列名获取最新的雄辩关系
- powerbi - Power BI 回复 URL 与为应用程序配置的回复 URL 不匹配
- python - 使用sklearn从文件中提取数组