我是 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 中我安装了:
- 实体框架
- 微软.AspNet.Mvc
- 微软.AspNetCore.App
- 微软.NETCore.App
EDIT:
我还在构建过程中收到这些警告:
警告 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>
非常感谢您的宝贵时间。