/// <summary>
        /// 生成分类下拉-列表框,选中指定的项
        /// </summary>
        /// <param name="html"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static MvcHtmlString SelectList_Category(this HtmlHelper html, long selectedValue)
        {
            Data.IRepository _iRepository = new Data.DataRepository();
            StringBuilder sb = new StringBuilder();
            sb.Append("<select name='Category' id='Category'>");
            foreach (var i in _iRepository.GetModel<Category>())
            {
                if (i.ID == selectedValue && selectedValue != 0)
                    sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", i.ID, i.Name);
                else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", i.ID, i.Name);
            }
            sb.Append("</select>");
            return MvcHtmlString.Create(sb.ToString());
        }
        /// <summary>
        /// 生成分类下拉列表框
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString SelectList_Category(this HtmlHelper html)
        {
            return SelectList_Category(html, 0);
        }

前台调用:

@Html.SelectList_Category()

我们从代码中可以看到,这个扩展方法其实是对ViewPage页面类上的HtmlHelper对象进行的扩展,它的对象名称是Html,所以在继承了ViewPage或者ViewUserControl的页面中,都可以使用SelectList_Category这个扩展方法

相关文章:

  • 2021-11-18
  • 2021-08-04
  • 2021-09-27
  • 2021-07-03
  • 2021-07-12
  • 2021-06-02
  • 2021-12-05
  • 2021-10-28
猜你喜欢
  • 2021-06-28
  • 2022-12-23
  • 2021-04-27
  • 2022-02-26
  • 2021-12-09
  • 2021-08-19
相关资源
相似解决方案