Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

asp.net mvc - Razor CheckBoxFor on List<string>

I can't seem to figure out how to get a list of strings to render in my form as checkboxes. Tried a few things from various sites and questions/answers on here with no luck.

MODEL

    var model = new ProjectModel()
    {
        ProjectType = new List<string>()
        {
            "Brand Creation",
            "Web Design",
            "Graphic Design",
            "Custom Programming",
            "E-Commerce",
            "Other"
        },
    };

VIEW - I've tried the following ways.

                @foreach (var item in Model.ProjectType)
                {
                    @Html.CheckBoxFor(model => true, item)
                    <br />
                }

                    @foreach (var item in Model.Budget)
                    {
                        @Html.CheckBox(item)
                        <br />
                    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would rather use Enum to display checkboxes. I know it's not your answer but you may consider doing this.

You have to do these step >

  1. Enum

    [Flags]
    public enum ProjectType
    {
        [Display(Name = "Brand Creation")]
        BrandCreation = 1,
        [Display(Name = "Web Design")]
        WebDesign = 2
    }
    
  2. Create Html Extension method

     public static IHtmlString CheckboxListForEnum<T>(this HtmlHelper html, string name, T modelItems) where T : struct
        {
            StringBuilder sb = new StringBuilder();
    
            var displayAttributeType = typeof(DisplayAttribute);
    
            foreach (T item in Enum.GetValues(typeof(T)).Cast<T>())
            {
                FieldInfo field = item.GetType().GetField(item.ToString());
    
                DisplayAttribute attrs = (DisplayAttribute)field.
                              GetCustomAttributes(displayAttributeType, false).FirstOrDefault();
    
                TagBuilder builder = new TagBuilder("input");
                long targetValue = Convert.ToInt64(item);
                long flagValue = Convert.ToInt64(modelItems);
    
                if ((targetValue & flagValue) == targetValue)
                    builder.MergeAttribute("checked", "checked");
    
                builder.MergeAttribute("type", "checkbox");
                builder.MergeAttribute("value", attrs.GetName());
                builder.MergeAttribute("name", name);
                builder.InnerHtml = attrs.GetName();
    
                sb.Append(builder.ToString(TagRenderMode.Normal));
            }
    
            return new HtmlString(sb.ToString());
        }
    
  3. Finally in your View

    @Html.CheckboxListForEnum("ProjectType", @Model.ProjectType)
    
  4. Get posted value

    public ActionResult Index(FormCollection form)
    {
       string[] AllStrings = form["ProjectType"].Split(',');
    
      foreach (string item in AllStrings)
      {
         int value = int.Parse(item);
      }
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...