ASP.NET-核心嵌套ASP TagHelper

ASP.NET-Core nested ASP TagHelper

我在 ASP 核心应用程序开发方面有点陌生。我有一个页面,我有很多输入。

<div class="form-group">
   <label asp-for="Animal.Name"></label>
   <input asp-for="Animal.Name" class="form-control" />
   <span asp-validation-for="Animal.Name" class="text-danger"></span>
</div>

<div class="form-group">
   <label asp-for="Animal.Age"></label>
   <input asp-for="Animal.Age" class="form-control" />
   <span asp-validation-for="Animal.Age" class="text-danger"></span>
</div>

等等。我想通过 TagHelpers 实现类似的目标:

<input-helper value="Animal.Name" />
<input-helper value="Animal.Age" />

生成与上层相同的结果。我能以某种方式做到这一点吗?

根据您的描述,我建议您可以使用IHtmlGenerator来生成label asp-forinput asp-forasp-validation-for taghelper的代码。

更多详情,您可以参考以下代码:

标签助手:

using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace SecurityRelatedIssue
{
    [HtmlTargetElement("input-helper")]
    public class inputhelper : TagHelper
    {
        [HtmlAttributeName("asp-for")]
        public ModelExpression For { get; set; }

        private readonly IHtmlGenerator _generator;

        [ViewContext]
        public ViewContext ViewContext { get; set; }

        public inputhelper(IHtmlGenerator generator)
        {
            _generator = generator;
        }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            using (var writer = new StringWriter())
            {
                writer.Write(@"<div class=""form-group"">");

                var label = _generator.GenerateLabel(
                                ViewContext,
                                For.ModelExplorer,
                                For.Name, null,
                                new { @class = "control-label" });

                label.WriteTo(writer, NullHtmlEncoder.Default);

                var textArea = _generator.GenerateTextBox(ViewContext,
                                    For.ModelExplorer,
                                    For.Name,
                                    For.Model,
                                    null,
                                    new { @class = "form-control" });

                textArea.WriteTo(writer, NullHtmlEncoder.Default);

                var validationMsg = _generator.GenerateValidationMessage(
                                        ViewContext,
                                        For.ModelExplorer,
                                        For.Name,
                                        null,
                                        ViewContext.ValidationMessageElement,
                                        new { @class = "text-danger" });

                validationMsg.WriteTo(writer, NullHtmlEncoder.Default);

                writer.Write(@"</div>");

                output.Content.SetHtmlContent(writer.ToString());

            }

        }
    }
}

_ViewImports.cshtml

中注册标签助手
@addTagHelper *,{yourapplicationnamespace}

用法:

<input-helper asp-for="@Model.str" ></input-helper>

结果: