gpt4 book ai didi

c# - Html Helper 'Action' 未定义,Asp.NET Core 2.2

转载 作者:行者123 更新时间:2023-12-05 02:14:53 24 4
gpt4 key购买 nike

我是 ASP.NET Core MVC 的新手。在这个问题的开头,我会说我已经阅读了其他类似的问题,但它们与我遇到的问题并不完全相同。

我正在尝试实现一个动态的左侧导航,其中每个按钮都是从我的数据库中加载的。

当我调用 @Html.Action("NavMenu", "Nav")在我的“_Layout.cshtml”中出现错误

Error CS1929
'IHtmlHelper' does not contain a definition for 'Action' and the best extension method overload 'UrlHelperExtensions.Action(IUrlHelper, string, object)' requires a receiver of type 'IUrlHelper'

其他类似问题之一让我添加了 @using System.Web.Mvc.Html到我的“_ViewImports”的顶部,这会导致错误:

CS7069: Reference to type 'RouteValueDictionary' Claims it is defined in 'System.Web', but it could not be found

还有答案说@html.Action()应该内置到 Core 2.0 中

使用 @await Html.PartialAsync("_SideNav")仅适用于我的索引 View ,因为该 View 具有 @model IEnumerable<HSCWebApp.Models.HSCTable>但我的其他观点需要@model HSCWebApp.Models.HSCTable ,这会导致“_SideNav”崩溃

我 100% 愿意接受其他实现方式。

在我安装的 NuGet 中:

  • Entity Framework
  • 微软.AspNet.Mvc
  • Microsoft.AspNetCore.App
  • Microsoft.NETCore.App

编辑:

我在构建过程中也收到了这些警告:

Warning NU1701 Package 'Microsoft.AspNet.Mvc 5.2.2' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.1'. This package may not be fully compatible with your project.

与 Entity Framework 、AspNet.Razor AspNet.Webpages 一起

这些警告会以某种方式影响我的引用吗?


文件如下:

Controller :(“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组件,正如@Llazar 所建议的那样,它起作用了!

我的 View 组件:

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")

没有其他需要改变的。感谢您的帮助!

关于c# - Html Helper 'Action' 未定义,Asp.NET Core 2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52972732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com