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

 

posted on 2017-05-09 11:07  月&&生  阅读(171)  评论(0编辑  收藏  举报