如何使用MVC中的复选框列表

时间:2015-04-09 10:54:43

标签: asp.net-mvc

我需要显示包含多个选项的复选框列表。用户必须选中最小一个复选框,用户可以选中多个复选框。

我想将所有复选框(已选中)的值存储在一个字段中作为字符串(数据库)以逗号分隔。这不是强制性的,强制性需要存储每个选中复选框的多个值。欢迎使用替代解决方案。

  

模型

public class Member
{
      public string Member_VehicalType { get; set; }
      public IList<SelectListItem> Member_VehicalType_List { get; set; }  
  

控制器

    [HttpGet]
    public ActionResult Create()
    {
        Member objMemberModel = new Member();            
        List<SelectListItem> vehical_Types = new List<SelectListItem>();
        vehical_Types.Add(new SelectListItem { Text = "Two Wheeler", Value = "1" });
        vehical_Types.Add(new SelectListItem { Text = "Four Wheeler", Value = "2" });
        objMemberModel.Member_VehicalType_List = vehical_Types;
        return View(objMemberModel);

如何使用@Html.CheckBoxFor(@Html.CheckBox(

创建视图

1 个答案:

答案 0 :(得分:0)

我最近不得不将SelectListItem作为复选框列表处理。我提出了以下HtmlExtensions,这可能会有所帮助。这些扩展提供与@Html.DropDownListFor(model => ...);

相同的功能

用法:@Html.CheckBoxListFor(model => model.ModelMemberToPutValueIn, Model.Member_VehicalType_List)

public static class HtmlExtensions
{
    public static MvcHtmlString CheckBoxListFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, IEnumerable<SelectListItem> items, object htmlAttributes = null)
    {
        var listName = ExpressionHelper.GetExpressionText(expression);
        var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        items = GetCheckboxListWithDefaultValues(metaData.Model, items);

        return htmlHelper.CheckBoxList(listName, items, htmlAttributes);
    }

    public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string listName, IEnumerable<SelectListItem> items, object htmlAttributes = null)
    {
        if (items == null) return null;

        var container = new TagBuilder("div");
        container.AddCssClass("checkbox-list");
        container.MergeAttribute("id", HtmlHelper.GenerateIdFromName(listName));

        foreach (var item in items)
        {
            var id = HtmlHelper.GenerateIdFromName(String.Join(".", listName, item.Text));
            var div = new TagBuilder("div");
            div.AddCssClass("checkbox");

            var cb = new TagBuilder("input");
            cb.MergeAttribute("type", "checkbox");
            cb.MergeAttribute("id", id);
            cb.MergeAttribute("name", listName);
            cb.MergeAttribute("value", item.Value ?? item.Text);
            if (item.Selected) cb.MergeAttribute("checked", "checked");

            var label = new TagBuilder("label");
            label.MergeAttribute("for", id);
            label.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
            label.InnerHtml = item.Text;

            div.InnerHtml = cb.ToString(TagRenderMode.SelfClosing) + label;
            container.InnerHtml += div;
        }

        return new MvcHtmlString(container.ToString());
    }

    private static IEnumerable<SelectListItem> GetCheckboxListWithDefaultValues(object defaultValues, IEnumerable<SelectListItem> selectList)
    {
        var defaultValuesList = defaultValues as IEnumerable;
        if (defaultValuesList == null) return selectList;

        var values = from object value in defaultValuesList select Convert.ToString(value, CultureInfo.CurrentCulture);
        var selectedValues = new HashSet<string>(values, StringComparer.OrdinalIgnoreCase);
        var newSelectList = new List<SelectListItem>();

        foreach (var item in selectList)
        {
            item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text);
            newSelectList.Add(item);
        }

        return newSelectList;
    }
}