Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

c# - NameAndIdProvider finds no reference in the context of the class - ASP.NET Core MVC

I need to make a custom implementation for the input field, so I created the following class:

[HtmlTargetElement("input", Attributes = "tm-for", TagStructure = TagStructure.WithoutEndTag)]
public class TmInputTagHelper : InputTagHelper
{
    
    public TmInputTagHelper(IHtmlGenerator generator) : base(generator)
    {            
    }

    [HtmlAttributeName("tm-for")]
    public ModelExpression TmFor
    {
        get => For;
        set => For = value;
    }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        base.Process(context, output);


        string fullName = NameAndIdProvider.GetFullHtmlFieldName(ViewContext, For.Name);

        if (ViewContext.ViewData.ModelState.TryGetValue(fullName, out var entry) && entry.Errors.Count > 0)
        {
            output.AddClass("is-invalid", HtmlEncoder.Default);
            output.RemoveClass("input-validation-error", HtmlEncoder.Default);
        }
    }
}

However, I can't find the reference for this class: NameAndIdProvider:

string fullName = NameAndIdProvider.GetFullHtmlFieldName(ViewContext, For.Name);

I have the following usings:

using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Text.Encodings.Web;

Well, I don't know how I can get past that anymore!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may use asp.net core 3 or higher, but NameAndIdProvider is an assembly below 2.2, which is the source code.

The scope of Internal is limited to a certain assembly, but the assembly here is different, so it cannot be referenced. A better way is to customize this function in current project according to above GetFullHtmlFieldName.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...