TagHelper drop-down box

1. Create registration data

1)Startup.cs

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddHttpContextAccessor();
            services.AddTransient<ICountryManager, CountryManager>();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();

            app.UseMvc();

        }
    }

2)Model

public class Country
    {
        /// <summary>
        /// 编码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
    }

3)Data

using CoreTest.Model;
using System.Collections.Generic;

namespace CoreTest.Manager
{
    public interface ICountryManager
    {
        IEnumerable<Country> GetAll();
    }

    public class CountryManager : ICountryManager
    {
        public readonly List<Country> _countryList;

        public CountryManager()
        {
            _countryList = new List<Country>
            {
                new Country{ Code="001",Name="China" },
                new Country{ Code="002",Name="Japan" },
                new Country{ Code="003",Name="USA" },
            };
        }

        public IEnumerable<Country> GetAll()
        {
            return _countryList;
        }
    }

}

4) TagHelper the drop-down box control object

    [HtmlTargetElement("country-list")]
    public class CountryListTagHelper:TagHelper
    {
        private readonly ICountryManager _countryManager;

        public string SelectedValue { get; set; }

        public CountryListTagHelper(ICountryManager countryManager)
        {
            _countryManager = countryManager;
        }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "select";
            output.Content.Clear();
            foreach (var item in _countryManager.GetAll())
            {
                var selected = "";
                if (SelectedValue != null && SelectedValue.Equals(item.Code, StringComparison.CurrentCultureIgnoreCase))
                {
                    selected = "selected=\"selected\"";
                }
                var listItem = $"<option value=\"{item.Code}\" {selected}>{item.Name}</option>";
                output.Content.AppendHtml(listItem);
             }
        }
    }

5) References on the page

@page
@model CoreTest.Pages.EditModel
@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper * , CoreTest
@{
}
<form method="post">
    <p>
        <label>姓名:</label>
        <input type="text" asp-for="@Model.Input.Name" />
    </p>
    <p>
        <Label> Age: </ label>
        <input type="number" asp-for="@Model.Input.Age" />
    </p>
    <p>
        <Label> Sex: </ label>
        <select asp-for="@Model.Input.Sex"></select>
    </p>
    <p>
        <Label> Phone number: </ label>
        <input type="text" asp-for="@Model.Input.Phone" />
    </p>
    <p>
        <label>国籍:</label>
        <country-list selected-value="002"></country-list>
    </p>
</form>

 

effect:

 

 

 

 

Note:

1) Startup remember the registration interface service object (red section 1.1)

2) on a separate page TagHelperHelper

@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers

3) Use the project all TageHelper (@addTagHelper typeName, AssemblyName)

@addTagHelper *, CoreTest
// Mode 1, under a program set all Taghelper 
@addTagHelper * , CoreTest

// Second way, explicitly specify the path 
@addTagHelper " CoreTest.CountryListTagHelper, CoreTest "

// Three ways, fuzzy matching 
@addTagHelper " CoreTest.CountryList *, CoreTest "

 

 

Thanks: https: //www.cnblogs.com/hager/p/5817335.html




 

Guess you like

Origin www.cnblogs.com/dzw159/p/12008693.html