【问题标题】:How to include the extension on class如何在类中包含扩展
【发布时间】:2021-08-22 12:06:25
【问题描述】:

我正在尝试从 .NET 4.5(旧)迁移到 .NET Core 3.1。 (新)

在旧版本中,我有一个使用 System.web.Mvc 命名空间的类 MvcExtensionMethods。

 namespace System.Web.Mvc
{
    public static class MvcExtensionMethods
    {
       
       
        public static MvcForm BeginEditForm(this AjaxHelper helper, string formId = "addform")
        {
            var queryString = HttpContext.Current.Request.QueryString;

我的 cshtml 文件:

    @model QualityGateWeb.Models.CriteriaRefViewModel

@{
    var ctxModel = "Criteria Referential";
    ViewBag.Title = Model.Id == 0 ? "New " + ctxModel : ctxModel + " - " + Model.Code;

    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script>
    @{
        var modelId = @Model.Id;
    }
</script>

@using (Html.BeginEditForm("addform", true))
{

在我的前端 cshtml 中,我可以像这样使用这个类作为扩展:

当我将鼠标移到方法 BeginEditForm 上时,您可以在图像上看到(扩展名)。

当我悬停时,Html 是

在 .NET 4.5 中不再有 System.web,所以我改用 Microsoft.AspNetCore.Mvc.Rendering

using System.Text.Json;

namespace Microsoft.AspNetCore.Mvc.Rendering
{
    public static class MvcExtensionMethods
    {
        private static LinkGenerator _linkGenerator;
        private static HttpContextAccessor _httpContextAccessor;
        private static HttpContext _HttpContext;

 

        /// <summary>
        /// Create a Form that preserv URL parameter
        /// </summary>
        public static MvcForm BeginEditForm(this HtmlHelper helper, string formId = "addform")
        {

但它找不到我的方法 BeginEditForm。 有什么想法吗?

【问题讨论】:

  • 请将代码和数据添加为文本(使用代码格式),而不是图像。图片:A)不允许我们复制粘贴代码/错误/数据进行测试; B) 不允许根据代码/错误/数据内容进行搜索; C) 视觉障碍用户难以清晰阅读以及更多原因。除了代码格式的文本之外,只有在图像添加了一些重要的东西,而不仅仅是文本代码/错误/数据传达的内容时,才应该使用图像。

标签: .net .net-core asp.net-core-mvc


【解决方案1】:

您的扩展是针对 HtmlHelper 类的,但 razor 中的 Html 对象是 IHtmlHelper 接口。只需将您的扩展更改为界面的扩展即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    相关资源
    最近更新 更多