I know there is a ListBoxFor
extension method among the ASP.NET MVC Html helper extension methods, but I always thought that a checkbox list is more user-friendly than a list box.
There was a very convenient CheckBoxList
control in good old WebForms, but obviously that is out of the picture now.
The question is, why is there no way in ASP.NET MVC to create a check box list? How can I write my own extension method that creates a check box list and behaves in a similar way ListBoxFor
behaves?
Here is a strongly typed HtmlHelper for CheckBoxListFor that handles selected items as an array in your viewdata model. I chose not to wrapper the Html.CheckBox or Html.CheckBoxFor methods as I don't want the hidden "false" fields in my checkbox lists.
Please feel free to improve on this and repost :-)
//View
<%: Html.CheckBoxListFor(model => model.FreightTypeIds, FreightTypeMultiSelectList) %>
//Controller
public ActionResult SomeAction(int[] FreightTypeIds)
{
//...
return View();
}
//Extension
public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, IEnumerable<TProperty>>> expression, MultiSelectList allOptions, object htmlAttributes = null)
{
ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, IEnumerable<TProperty>>(expression, htmlHelper.ViewData);
// Derive property name for checkbox name
string propertyName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(modelMetadata.PropertyName);
// Get currently select values from the ViewData model
IEnumerable<TProperty> list = expression.Compile().Invoke(htmlHelper.ViewData.Model);
// Convert selected value list to a List<string> for easy manipulation
IList<string> selectedValues = new List<string>();
if (list != null)
{
selectedValues = new List<TProperty>(list).ConvertAll<string>(delegate(TProperty i) { return i.ToString(); });
}
// Create div
TagBuilder divTag = new TagBuilder("div");
divTag.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
// Add checkboxes
foreach (SelectListItem item in allOptions)
{
divTag.InnerHtml += string.Format(
"<div><input type=\"checkbox\" name=\"{0}\" id=\"{1}_{2}\" " +
"value=\"{2}\" {3} /><label for=\"{1}_{2}\">{4}</label></div>",
propertyName,
TagBuilder.CreateSanitizedId(propertyName),
item.Value,
selectedValues.Contains(item.Value) ? "checked=\"checked\"" : string.Empty,
item.Text);
}
return MvcHtmlString.Create(divTag.ToString());
}