码迷,mamicode.com
首页 > Web开发 > 详细

net netcore2 netcore3 HtmlHelper扩展(checkboxlistfor)为例

时间:2020-01-10 10:43:37      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:split   typeof   ati   item   checkbox   tostring   values   form   lda   

.net中以扩展CheckBoxListFor为例

 

     public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items, object htmlAttributes)
        {
            var result = new StringBuilder();
            string name = ExpressionHelper.GetExpressionText(expression);
            var modelData = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData).Model;
            List<string> currentValues = StringHelper.StringSplit<string>(modelData == null ? string.Empty : modelData.ToString());
            foreach (var item in items)
            {
                if (currentValues.Contains(item.Value))
                    result.AppendFormat("<label class=\"checkbox-inline checkbox-styled\"><input type=\"checkbox\" name=\"{0}\" value=\"{1}\" checked><span class=‘text‘>{2}</span></label>", name, item.Value, item.Text);
                else
                    result.AppendFormat("<label class=\"checkbox-inline checkbox-styled\"><input type=\"checkbox\" name=\"{0}\" value=\"{1}\"><span class=‘text‘>{2}</span></label>", name, item.Value, item.Text);
            }
            return MvcHtmlString.Create(result.ToString());
        }

netcore 2中以扩展CheckBoxListFor为例

 

    public static IHtmlContent CheckBoxListFor<TModel, TProperty> (this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items, object htmlAttributes)
        {
            var result = new StringBuilder();
            string name = ExpressionHelper.GetExpressionText(expression);
            var modelData = ExpressionMetadataProvider.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData, htmlHelper.MetadataProvider).Model;
            List<string> currentValues = StringHelper.StringSplit<string>(modelData == null ? string.Empty : modelData.ToString());
            foreach (var item in items)
            {
                if (currentValues.Contains(item.Value))
                    result.AppendFormat("<label class=\"checkbox-inline checkbox-styled\"><input type=\"checkbox\" name=\"{0}\" value=\"{1}\" checked><span class=‘text‘>{2}</span></label>", name, item.Value, item.Text);
                else
                    result.AppendFormat("<label class=\"checkbox-inline checkbox-styled\"><input type=\"checkbox\" name=\"{0}\" value=\"{1}\"><span class=‘text‘>{2}</span></label>", name, item.Value, item.Text);
            }
            return new HtmlString(result.ToString());
        }

  

netcore 3中以扩展CheckBoxListFor为例

 

     public static IHtmlContent CheckBoxListFor<TModel, TProperty> (this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items, object htmlAttributes)
        {
            var result = new StringBuilder();
            ModelExpressionProvider modelExpressionProvider = (ModelExpressionProvider)htmlHelper.ViewContext.HttpContext.RequestServices.GetService(typeof(IModelExpressionProvider));
            var modelData = modelExpressionProvider.CreateModelExpression(htmlHelper.ViewData, expression).Model;
            var name=modelExpressionProvider.GetExpressionText(expression);

            List<string> currentValues = ((modelData == null) ? string.Empty : modelData.ToString()).Split<string>(Array.Empty<string>());

            foreach (var item in items)
            {
                if (currentValues.Contains(item.Value))
                    result.AppendFormat("<label class=\"checkbox-inline checkbox-styled\"><input type=\"checkbox\" name=\"{0}\" value=\"{1}\" checked><span class=‘text‘>{2}</span></label>", name, item.Value, item.Text);
                else
                    result.AppendFormat("<label class=\"checkbox-inline checkbox-styled\"><input type=\"checkbox\" name=\"{0}\" value=\"{1}\"><span class=‘text‘>{2}</span></label>", name, item.Value, item.Text);
            }
            return new HtmlString(result.ToString());
        }

  

net netcore2 netcore3 HtmlHelper扩展(checkboxlistfor)为例

标签:split   typeof   ati   item   checkbox   tostring   values   form   lda   

原文地址:https://www.cnblogs.com/zzxulong/p/12174575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!