Select Tag Helper in ASP.NET Core MVC

前端 未结 6 2237
再見小時候
再見小時候 2020-11-22 04:35

I need some help with the select tag helper in ASP.NET Core.

I have a list of employees that I\'m trying to bind to a select tag helper. My employees are in a

6条回答
  •  自闭症患者
    2020-11-22 05:28

    I created an Interface and a tag helper for this. So I didn't have to convert the IEnumerable items into IEnumerable every time I have to populate the

    And to make it work with the tag helper you have to implement that interface in your class:

    public class Employee : IIntegerListItem
    {
       public int Id { get; set; }
       public string FullName { get; set; }
    
       public int Value { return Id; }
       public string Text{ return FullName ; }
    }
    

    These are the needed codes:

    The interface:

    public interface IIntegerListItem
    {
        int Value { get; }
        string Text { get; }
    }
    

    The tag helper:

    [HtmlTargetElement("options", Attributes = "asp-items")]
    public class OptionsTagHelper : TagHelper
    {
        public OptionsTagHelper(IHtmlGenerator generator)
        {
            Generator = generator;
        }
    
        [HtmlAttributeNotBound]
        public IHtmlGenerator Generator { get; set; }
    
        [HtmlAttributeName("asp-items")]
        public object Items { get; set; }
    
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.SuppressOutput();
            // Is this  element a child of a 
                            
        
    提交评论

提交回复
热议问题