【发布时间】:2020-10-06 05:14:18
【问题描述】:
我为我的网络应用程序中的选择元素创建了一个标签助手:
[HtmlTargetElement( "select", Attributes = nameof( AutoPostBack ) )]
public class AutoPostBackTagHelper : TagHelper
{
public bool AutoPostBack { get; set; }
public override void Process( TagHelperContext context, TagHelperOutput output )
{
// AutoPostBack is always false
if ( AutoPostBack )
{
output.Attributes.SetAttribute( "onchange", "this.form.submit();" );
}
// here I could extract the attribute
Microsoft.AspNetCore.Html.HtmlString x =
(Microsoft.AspNetCore.Html.HtmlString)context.AllAttributes[nameof( AutoPostBack )].Value;
// and this is working
if ( x.Value == "true" )
{
output.Attributes.SetAttribute( "onchange", "this.form.submit();" );
}
}
}
这是我认为的用法:
<select asp-for="MessageType"
asp-items="Html.GetEnumSelectList<MyViewModels.MessageType>()"
class="form-control" AutoPostBack="true">
</select>
我想使用通过定义的属性AutoPostBack推荐的条件。为什么属性总是假的?
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-core tag-helpers