【发布时间】:2016-04-23 04:26:58
【问题描述】:
我正在尝试使用 .Net Core RC1 为 MVC 6 创建标签助手。我找到了一些不错的sources,但不是很接近,this 是我找到的最接近的,并采用了我认为创建现有代码所需的元素:
首先是我的目标 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>
现在我只是想显示一个输入标签。如果我能弄清楚,我将添加一个循环来获取其他人。这是我的 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 />
所以它正在拾取标签并处理它,但不像我预期的那样。在我出错的地方提供帮助将不胜感激。
【问题讨论】:
标签: c# asp.net-core-mvc tag-helpers