MVC设计及使用拓展

一、MVC HtmlHelper拓展示例

添加拓展类文件
 public static class Html
    {
        public static Dictionary<int, string> TestExtensionHtml = new Dictionary<int, string>()
        {
            {1,"请选择"},
            {2,"北京市"},
            {3,"上海市"}
        };
        public static MvcHtmlString TestDropDownList(this HtmlHelper html)
        {
            string result = string.Empty;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<select id="test" name="test"");
            foreach (var item in TestExtensionHtml)
            {
                
                sb.AppendLine(string.Format("<option value="{0}" >{1} </option>", item.Key, item.Value));
            }
            sb.AppendLine("</select>");
            result = sb.ToString();
            return new MvcHtmlString(result);
        }
    }

视图引用该命名空间
@using Demo.Extensions

添加拓展控件
@Html.TestTestDropDownList();
View Code
原文地址:https://www.cnblogs.com/eric-gms/p/6829633.html