日期:2014-05-17  浏览次数:20719 次

使用HtmlHelper为ASP.NET MVC3扩展CheckBoxList

扩展

        public static IEnumerable<KeyValuePair<string, MvcHtmlString>> CheckBoxListFor<TModel, TProperty>(
            this HtmlHelper<TModel> html,
            Expression<Func<TModel, TProperty>> selectedItemsExpression,
            ICollection<string> allItems,
            IDictionary<string, object> htmlAttributes = null)
        {
            var selectedItems = (ICollection<string>)typeof(TModel).GetProperty(ExpressionHelper.GetExpressionText(selectedItemsExpression)).GetValue(html.ViewData.Model);
            foreach (var item in allItems)
            {
                var tag = new TagBuilder("input");
                tag.MergeAttribute("type", "checkbox");
                tag.MergeAttribute("name", ExpressionHelper.GetExpressionText(selectedItemsExpression));
                tag.MergeAttribute("value", item);
                if (selectedItems.Contains(item))
                    tag.MergeAttribute("checked", "checked");
                tag.MergeAttributes(htmlAttributes);

                yield return new KeyValuePair<string, MvcHtmlString>(item, new MvcHtmlString(tag.ToString()));
            }
        }


使用

数据模型

    /// <summary>
    /// 参数
    /// </summary>
    public class Parameters
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Editable(false)]
        [ScaffoldColumn(false)]
        [Display(Name = "标识")]
        public int Id { get; set; }

        [Display(Name = "总标题")]
        [ViewBlock]
        public string 总标题 { get; set; }

        [Display(Name = "页眉")]
        [ViewBlock]
        public string 页眉 { get; set; }

        [Display(Name = "页脚")]
        [ViewBlock]
        public string 页脚 { get; set; }

        [Display(Name = "产品类别")]
        [ViewBlock]
        public string 产品类别 { get; set; }

        [Display(Name = "产品")]
        [ViewBlock]
        public string 产品 { get; set; }

        [Display(Name = "新闻")]
        [ViewBlock]
        public string 新闻 { get; set; }
    }

    /// <summary>
    /// 用于显示可作为显示块
    /// </summary>
    public class ViewBlockAttribute : Attribute
    { }

    /// <summary>
    /// 显示块
    /// </summary>
    public class ViewBlock
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Editable(false)]
        [ScaffoldColumn(false)]
        [Display(Name = "标识")]
        public int Id { get; set; }

        [Display(Name = "标题")]
        public string Title { get; set; }
        [Display(Name = "内容")]
        public string Content { get; set; }
        [Display(Name = "索引")]
        public int Index { get; set; }
        [Display(Name = "是否作为主导航菜单项")]
        public bool AsMainMenuItem { get; set; }
        [Display(Name = "说明")]
        public string Comment { get; set; }


        [Display(Name = "子显示块")]
        public virtual ICollection<ViewBlock> ViewBlocks { get; set; }

        [Display(Name = "参数", Description = "可以将参数作为内容的一部分。通过分隔符来分隔各个元素")]
        [ScaffoldColumn(false)]
        public string Parameters { get; set; }
        [Display(Name = "参数", Description = "可以将参数作为内容的一部分。通过分隔符来分隔各个元素")]
        [ScaffoldColumn(false)]
        public List<string> FormatedParameters
        {
            get { return (Parameters == null) ? null : Parameters.Split(new[] { Properties.Resources.分隔符 }, StringSplitOptions.RemoveEmptyEntries).ToList(); }
            set { Parameters = (value == null) ? null : value.Aggregate("", (b, r) => b + Properties.Resource