编辑器模板中的标签助手asp-for

时间:2018-11-16 14:35:58

标签: c# asp.net-core mvc-editor-templates tag-helpers asp.net-core-tag-helpers

我有自己的自定义标记帮助程序,用于<select> HTML元素。在普通视图中使用时,它可以正常工作,但在编辑器模板中使用时,它会错误地设置name属性。内置的标记助手似乎正常工作。

应将其设置为:Object.PropertyName,但应将其设置为PropertyName

我正在使用ModelExpression.Name在代码末尾设置name属性。我应该使用其他东西吗?

[HtmlTargetElement("select", Attributes = "asp-for")]
public class SelectOptionTagHelper : TagHelper
{
    private readonly IPersonalTitleRepository _personalTitleRepository;

    public SelectOptionTagHelper(IPersonalTitleRepository personalTitleRepository)
    {
        _personalTitleRepository = personalTitleRepository;
    }

    public override int Order { get; } = int.MaxValue;

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

    [HtmlAttributeName("asp-items")]
    public IEnumerable<SelectListItem> AspItems { get; set; }

    private Dictionary<string, string> GetDropdownListValues(ListOptions option)
    {
        Dictionary<string, string> dict;
        switch (option)
        {
            case ListOptions.Titles:
                dict = _personalTitleRepository.GetAll()
                    .ToDictionary(x => x.PersonalTitleId.ToString(), x => x.Title);
                break;
            default:
                throw new Exception("ListOptions enum has not been added to SelectOptionTaghelper");

        }

        return dict;
    }



    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        //if it is normal use of select list - process as normal
        if (AspItems != null)
        {
            base.Process(context, output);
            return;
        }

        string selected = "";

        if (AspFor.Model != null)
        {
            selected = AspFor.Model.ToString();
        }

        //get the SelectList attribute
        var type = AspFor.Metadata.ContainerType; //Get Object Type
        var propertyName = AspFor.Metadata.PropertyName; //Get property name like "FundingId" (excludes full name like Application[i].FundingId)
        var property = type.GetProperty(propertyName);
        var attribute = property.GetCustomAttribute(typeof(SelectListAttribute)) as SelectListAttribute;

        if (attribute == null)
        {
            throw new Exception("You must either supply an SelectList to asp-items or have a [SelectList(ListOptions.Option)] attribute");
        }

        Dictionary<string, string> dict = GetDropdownListValues(attribute.Option);
        output.Content.AppendHtml($"<option value=\"\" disabled selected>Please select an option</option>");

        //build option list
        foreach (var item in dict)
        {
            if (!string.IsNullOrWhiteSpace(selected) && item.Key == selected)
            {
                output.Content.AppendHtml($"<option value=\"{item.Key}\" selected>{item.Value}</option>");
            }
            else
            {
                output.Content.AppendHtml($"<option value=\"{item.Key}\">{item.Value}</option>");
            }
        }

        //Issue is here - setting the name property
        output.Attributes.SetAttribute("Name", AspFor.Name);
        output.Attributes.SetAttribute("Id", AspFor.Name);



    }


}

0 个答案:

没有答案