如何编写包含其他taghelpers的自定义ASP.NET 5 taghelper [英] How to write a custom ASP.NET 5 taghelper that contains other taghelpers

查看:165
本文介绍了如何编写包含其他taghelpers的自定义ASP.NET 5 taghelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看Google的taghelpers上的示例,但找不到我想要的任何示例.

I've been looking at examples on taghelpers on google but couldn't find any example I'm looking for.

我有以下代码:

<div class="form-group">
    <label asp-for="PersonName" class="col-md-2 control-label"></label>
    <div class="col-md-10">
        <input asp-for="PersonName" class="form-control" />
        <span asp-validation-for="PersonName" class="text-danger"></span>
    </div>
</div>

我想做的是用类似的东西代替它

What i'd look to do is replace it with something like

<bootstraprow asp-for="PersonName"></bootstraprow>

但是我不确定是否要编写包含其他taghelper的taghelper

However I'm not sure to write a taghelper that contains other taghelpers

  1. 有可能吗?
  2. 如果可能,请提供上述代码示例

它不同于存储变量在自定义标签助手中,但我想调用其他自定义标签助手或现有的标签助手.

It is not the same as storing variables in custom taghelpers but I want to call other custom taghelpers or existing taghelpers.

推荐答案

如果我们检查您拥有的内容,则您正在使用的唯一属性是PersonName.至于标记本身,其他所有内容都是好的旧HTML.

If we check what you have, the only property that you are using is PersonName. As for the markup itself, everything else is good old HTML.

因此您不需要更换任何东西.您需要的是依赖于IHtmlGenerator的构造函数.这将自动注入,您将能够根据模型生成不同的标签.

So you don't need to replace anything. What you need is to have constructor that has a dependency on IHtmlGenerator. This will get automatically injected and you will be able to generate the different tags based on your model.

相关的IHtmlGenerator签名:

public interface IHtmlGenerator
{
    ...

    TagBuilder GenerateValidationMessage(
        ViewContext viewContext,
        string expression,
        string message,
        string tag,
        object htmlAttributes);
    TagBuilder GenerateLabel(
        ViewContext viewContext,
        ModelExplorer modelExplorer,
        string expression,
        string labelText,
        object htmlAttributes);
    TagBuilder GenerateTextBox(
        ViewContext viewContext,
        ModelExplorer modelExplorer,
        string expression,
        object value,
        string format,
        object htmlAttributes);
    ...
}

就是这样!

这里有一些代码可以捕获基本标签:

Here's a bit of code that would capture the basic tag:

[HtmlTargetElement("bootstraprow")]
public BootstrapRowTagHelper: TagHelper
{
    protected IHtmlGenerator Generator { get; set; }
    public InputTagHelper(IHtmlGenerator generator)
    {
        Generator = generator;
    }

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

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

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        //todo: write your html generating code here.
    }
}

这是带有示例代码的存储库,该示例代码可从TagHelpers生成Bootstrap HTML:

Here's a repo with sample code that generates Bootstrap HTML from TagHelpers:

https://github.com/dpaquette/TagHelperSamples/blob/master/TagHelperSamples/src/TagHelperSamples.Bootstrap/

这篇关于如何编写包含其他taghelpers的自定义ASP.NET 5 taghelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆