当前位置: 代码迷 >> 综合 >> net netcore2 netcore3 HtmlHelper扩展(checkboxlistfor)为例
  详细解决方案

net netcore2 netcore3 HtmlHelper扩展(checkboxlistfor)为例

热度:74   发布时间:2024-01-27 05:22:51.0

.net中以扩展CheckBoxListFor为例

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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为例

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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为例

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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());

   }