使用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.Resources.分隔符 + r); }
        }
        /// <summary>
        /// 可选的参数值
        /// </summary>
        public List<string> ParameterItems
        {
            get
            {
                return typeof(Parameters).GetProperties()
                    .Where(p => p.GetCustomAttribute(typeof(ViewBlockAttribute)) != null)
                    .Select(p => p.Name)
                    .ToList();
            }
        }

        [Display(Name = "产品目录", Description = "可以将产品目录作为内容的一部分")]
        public virtual ICollection<ProductFolder> ProductFolders { get; set; }
    }

    /// <summary>
    /// 产品目录
    /// </summary>
    public class ProductFolder
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Editable(false)]
        [ScaffoldColumn(false)]
        [Display(Name = "标识")]
        public int Id { get; set; }

        [Display(Name = "名称")]
        public string Name { get; set; }

        [Display(Name = "备注")]
        public string Comment { get; set; }

        [Display(Name = "子目录")]
        public virtual ICollection<ProductFolder> Subs { get; set; }
    }


控制层

        public ActionResult ViewBlock(int id)
        {
            var item = _db.ViewBlocks.Find(id);
            return View("ViewBlock", item);
        }


显示层

            <table>
                @foreach (var p in @Html.CheckBoxListFor(m => m.FormatedParameters, Model.ParameterItems))
                {
                    <tr>
                        <td>@p.Key</td>
                        <td>@p.Value</td>
                    </tr>
                }
            </table>


效果


posted @ 2012-10-13 13:32  beta2013  阅读(235)  评论(0编辑  收藏  举报