@Html.Action() 不可用

The @Html.Action() is not available

我想在 asp.net 核心 mvc 中使用部分视图:

我想在 @Html.Action("ActionMethod","ControllerName")@Html.RenderAction("ActionMethod","ControllerName") 的视图中调用局部视图的操作方法。但它们不可用,我在 vscode 中看到编译器错误。我怎么称呼它?

我的代码是:

<!DOCTYPE html>
<html lang="en">
<head>
   ...
</head>
<body>
     @Html.Action("TestPartial") 
</body>
</html>

并且编译错误是:

/.../PerojectName/Views/Home/Index.cshtml(10,7): error CS1929: 'IHtmlHelper' does not contain a definition for 'Action' and the best extension method overload 'UrlHelperExtensions.Action(IUrlHelper, string)' requires a receiver of type 'IUrlHelper' [/.../PerojectName/PartialView_Example.csproj]

同样在控制器中,我在 [ChildActionOnly] 中看到编译器错误。

我的代码是:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace PartialView_Example.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult TestPartial()
        {
            ViewBag.Text="It is test";
            return PartialView();            
        } 
    }
}

并且编译器错误是:

/.../ProjectName/Controllers/HomeController.cs(18,10): error CS0246: The type or namespace name 'ChildActionOnlyAttribute' could not be found (are you missing a using directive or an assembly reference?) /.../ProjectName/Controllers/HomeController.cs(18,10): error CS0246: The type or namespace name 'ChildActionOnly' could not be found (are you missing a using directive or an assembly reference?)

我在 linux ubuntu.

中使用 vscode 和 .Net Sdk 5.0.301

问题出在哪里?

ChildActionOnlyAttribute@Html.Action@Html.RenderAction 在 .NET Core MVC 中不可用。这些作为 ASP.NET MVC 框架的一部分提供。 ChildActionOnlyAttribute 可以从命名空间 System.Web.Mvc.

中使用

或者,在 .NET Core MVC 中,您可以使用视图组件。 Here 是 MS 文档的 link。