How to bind checkbox values to a list of ints?

前端 未结 1 764
南笙
南笙 2020-12-12 01:56

I am following this example

enter link description here

But when I submit, it says can\'t convert \'false\' to \'int\'

as I assume its the false or t

相关标签:
1条回答
  • 2020-12-12 02:35

    your model is going to need to contain both the checked values and all the possible values.

    public class TestViewModel
    {
        public Guid Id { get; set; }
        public IDictionary<Guid, String> AllCheckboxOptions { get; set; }
        public Guid[] CheckedOptions { get; set; }
    }
    

    I use Guids as PKs in my database so these are the IDs from the database

    I then created a number of extension methods to generate the html

        public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, String name, List<SelectListItem> listInfo,
            IDictionary<String, Object> htmlAttributes, Int32 columns)
        {
            if (String.IsNullOrEmpty(name))
                throw new ArgumentException("The argument must have a value", "name");
            if (listInfo == null)
                throw new ArgumentNullException("listInfo");
            if (!listInfo.Any())
                return new MvcHtmlString(String.Empty);
    
            var outerSb = new StringBuilder();
    
            for (Int32 i = 0; i < columns; i++)
            {
                var listBuilder = new TagBuilder("ul");
                if (columns > 1)
                    listBuilder.MergeAttribute("class", "column");
    
                var innerSb = new StringBuilder();
    
                var take = listInfo.Count % columns == 0
                    ? listInfo.Count / columns
                    : (listInfo.Count / columns) + 1;
    
                var items = listInfo.Skip(i * take).Take(take);
    
                foreach (var info in items)
                {
                    var inputBuilder = new TagBuilder("input");
                    if (info.Selected) inputBuilder.MergeAttribute("checked", "checked");
                    inputBuilder.MergeAttribute("type", "checkbox");
                    inputBuilder.MergeAttribute("value", info.Value);
                    inputBuilder.MergeAttribute("id", info.Value);
                    inputBuilder.MergeAttribute("name", info.Value);
    
                    var labelBuilder = new TagBuilder("label");
                    labelBuilder.MergeAttribute("for", @info.Value);
                    labelBuilder.InnerHtml = info.Text;
    
                    var listItemWrapper = new TagBuilder("li");
                    //may have to encode here. 
                    listItemWrapper.InnerHtml = inputBuilder.ToString(TagRenderMode.SelfClosing) + labelBuilder.ToString(TagRenderMode.Normal);
    
                    innerSb.Append(listItemWrapper.ToString(TagRenderMode.Normal));
                }
    
                listBuilder.InnerHtml = innerSb.ToString();
                outerSb.Append(listBuilder.ToString(TagRenderMode.Normal));
            }
    
            return new MvcHtmlString(outerSb.ToString());
        }
    

    then in the view you can call your extension method

        @Html.CheckBoxList("yourtypes", (from o in Model.AllCheckboxOptions
                                            select new SelectListItem
                                            {
                                                Text = o.Value,
                                                Selected = Model.CheckedOptions.Contains(o.Key),
                                                Value = o.Key.ToString()
                                            }).ToList())
    

    I've used this technique with both MVC3 and MVC4 with success.

    0 讨论(0)
提交回复
热议问题