结合TagHelper语句

时间:2017-10-11 06:56:24

标签: c# asp.net razorengine

我使用asp core 2.0 MVC并且经常使用以下形式的代码:

<form asp-controller="Foo" asp-action="Bar">                
  <div class="form-group">
     <label asp-for="Age"></label>
     <input asp-for="Age" class="form-control"/>
     <span asp-validation-for="Age" class="text-danger"></span>
  </div>

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

有没有办法在TagHelper类中组合标签,输入和span标签,只需编写如下内容:

  <div class="form-group">
    <foo for="Age">
  </div>

将产生以下内容:

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

避免一遍又一遍地为我的属性写这3个常用标签?

提前谢谢。

修改

Hey Isma,谢谢你的回答。 您的代码生成简单

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

HTML。

但我希望'Processed'版本像

<input asp-for="Age"/>

<input type="number" data-val="true" data-val-range="The field Age must be between 0 and 100." data-val-range-max="100" data-val-range-min="0" data-val-required="The Age field is required." id="Age" name="Age" value="" />

in html

有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:3)

这可能对您有用或至少让您朝着正确的方向前进,我使用自定义TagHelper,使用TagHelper实现生成其他元素并附加所有输出(可能有更好/更清晰的方法来执行此操作顺便说一句。)

[HtmlTargetElement("myfield")]
public class MyFieldTagHelper : TagHelper
{
    private IHtmlGenerator _htmlGenerator;
    public MyFieldTagHelper(IHtmlGenerator htmlGenerator)
    {
        _htmlGenerator = htmlGenerator;
    }

    public string LabelContent { get; set; }
    public string ValidationContent { get; set; }

    [HtmlAttributeName("asp-for")]
    public ModelExpression For { get; set; }

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

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        var labelContext = CrateTagHelperContext();
        var labelOutput = CrateTagHelperOutput("label");

        labelOutput.Content.Append(LabelContent);

        if (For != null)
        {
            labelOutput.Attributes.Add("for", For.Name);
        }

        var label = new LabelTagHelper(_htmlGenerator)
        {
            ViewContext = ViewContext
        };

        label.Process(labelContext, labelOutput);


        var inputContext = CrateTagHelperContext();
        var inputOutput = CrateTagHelperOutput("input");

        inputOutput.Attributes.Add("class", "form-control");

        var input = new InputTagHelper(_htmlGenerator)
        {
            For = For,
            ViewContext = ViewContext
        };

        input.Process(inputContext, inputOutput);

        var validationContext = CrateTagHelperContext();
        var validationOutput = CrateTagHelperOutput("span");

        validationOutput.Content.Append(ValidationContent);

        validationOutput.Attributes.Add("class", "text-danger");

        var validation = new ValidationMessageTagHelper(_htmlGenerator)
        {
            For = For,
            ViewContext = ViewContext
        };

        validation.Process(validationContext, validationOutput);

        output.TagName = "";
        output.Content.AppendHtml(labelOutput);
        output.Content.AppendHtml(inputOutput);
        output.Content.AppendHtml(validationOutput);
    }

    private static TagHelperContext CrateTagHelperContext()
    {
        return new TagHelperContext(
            new TagHelperAttributeList(),
            new Dictionary<object, object>(),
            Guid.NewGuid().ToString("N"));
    }

    private static TagHelperOutput CrateTagHelperOutput(string tagName)
    {
        return new TagHelperOutput(
            tagName,
            new TagHelperAttributeList(),
            (a,b) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent(string.Empty);
                return Task.FromResult<TagHelperContent>(tagHelperContent);
            });
    }
}

现在在您看来,您应该可以这样做:

 <myfield label-content="This is a label" validation-content="Validation content" asp-for="Age"></myfield> 

它会为你生成整个块。

请记住在您拥有标记帮助程序的地方注册程序集,即使您在Web应用程序中有它们,也可以通过编辑文件Views / _ViewImports.cshtml并添加以下行来执行此操作:

@addTagHelper *, YourAssemblyName

更多信息:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro