2

.Net Core RC1 を使用して MVC 6 用のタグ ヘルパーを作成しようとしています。いくつかの優れたソースを見つけましたが、非常に近いものはありませんでした。これは私が見つけた最も近いもので、既存のコードを作成するために必要だと思った要素を取りました:

これを開始するには、私のターゲット HTML です。

<span class="form-control">
                Highly Disagree
                <input type="radio" name="MakingSense" value=1 />
                <input type="radio" name="MakingSense" value=2 />
                <input type="radio" name="MakingSense" value=3 />
                <input type="radio" name="MakingSense" value=4 />
                <input type="radio" name="MakingSense" value=5 />
                Highly Agree
            </span>

現在、入力タグの1つを表示しようとしています。私がそれを理解できれば、ループを追加して他のものを取得します。ここに私のTagHelperがあります

[HtmlTargetElement("input", Attributes = LikertForAttributeName)]
public class LikertTagHelper : TagHelper
{
    private const string LikertForAttributeName = "likert-for";

    [HtmlAttributeName(LikertForAttributeName)]
    public string ModelField { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        var content = new StringBuilder();
        var input = new TagBuilder("input");
        input.MergeAttribute("type", "radio");
        input.MergeAttribute("name", ModelField);
        input.MergeAttribute("value", "1");
        content.AppendLine(input.ToString());
        output.Content.SetContent(content.ToString());
        output.PreContent.SetHtmlContent("<span class=\"form-control\"");
        output.PostContent.SetHtmlContent("</span>");
    }

}

これが私のかみそりのcshtmlです:

<input likert-for="CharacterUnderstanding" />

ただし、これはhtml出力としてのみ取得します。

<input />

そのため、タグを取得して処理していますが、期待どおりではありません。私が間違っていた場所で助けていただければ幸いです。

4

1 に答える 1

1

タグヘルパーの開始点は入力タグであるためcontent、自己終了入力タグになり始めます。

最初に行うことは、それを span タグに変換することです。次に例を示します。

//Replace initial output (an input tag) with a span
var outerTag = new TagBuilder("span");
outerTag.AddCssClass("form-control");
output.MergeAttributes(outerTag);
output.TagName = outerTag.TagName;
output.TagMode = TagMode.StartTagAndEndTag;

output は span タグであるため、内部コンテンツの追加を開始できます。

  • 非常に同意する、非常に同意しない ラベルは、前後のコンテンツとして追加できます (まだスパン内にあります)。

    //Add Pre/Post texts
    output.PreContent.SetHtmlContent("Highly Disagree");
    output.PostContent.SetHtmlContent("Highly Agree");
    
  • ラジオ ボタンは、スパンのコンテンツに追加できます。

    //Add the radio buttons
    for(var x =0; x<5; x++)
    {
        var input = new TagBuilder("input");
        input.MergeAttribute("type", "radio");
        input.MergeAttribute("name", ModelField);
        input.MergeAttribute("value", x.ToString());
        output.Content.Append(input);
    }
    

このタグ ヘルパーを配置すると、剃刀ビューに次の行が表示されます。

<input likert-for="CharacterUnderstanding" />

次のようにレンダリングされます。

<span class="form-control">
    Highly Disagree
    <input name="CharacterUnderstanding" type="radio" value="0">
    <input name="CharacterUnderstanding" type="radio" value="1">
    <input name="CharacterUnderstanding" type="radio" value="2">
    <input name="CharacterUnderstanding" type="radio" value="3">
    <input name="CharacterUnderstanding" type="radio" value="4">
    Highly Agree
</span>

補足として、文字列を受け入れるオーバーロードを使用してコンテンツを追加するときは注意が必要です。元のコードでは、行content.AppendLine(input.ToString());は実際にはタグ ビルダー コンテンツの代わりにMicrosoft.AspNet.Mvc.Rendering.TagBuilderを追加していました。

于 2016-04-23T10:28:31.030 に答える