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