Example #1
0
        public IEnumerable <CustomSelectListItem> ListRoles(string userName)
        {
            using (var context = new AspnetDBContext())
            {
                foreach (var item in from rec in context.aspnet_Roles
                         where rec.Description == null || rec.Description.Length == 0
                         orderby rec.RoleName
                         select rec)
                {
                    CustomSelectListItem selectItem = new CustomSelectListItem()
                    {
                        Text = item.RoleName, Value = item.RoleName, Selected = (userName != null && Roles.IsUserInRole(userName, item.RoleName))
                    };

                    selectItem.Children = new MultiSelectList(
                        (from rec in context.aspnet_Roles
                         where rec.Description == item.RoleName
                         orderby rec.RoleName
                         select rec),
                        "RoleName",
                        "RoleName",
                        null);

                    //foreach (var child in from rec in context.aspnet_Roles
                    //                      where rec.Description == item.RoleName
                    //                      orderby rec.RoleName
                    //                      select rec)
                    //{
                    //    selectItem.Child.Add(new CustomSelectListItem() { Text = child.RoleName, Value = child.RoleName, Selected = (userName != null && Roles.IsUserInRole(userName, child.RoleName)) });
                    //}
                    yield return(selectItem);
                }
            }
        }
Example #2
0
        public static MvcHtmlString CheckBox(this HtmlHelper helper, string name, CustomSelectListItem item, IDictionary <string, object> checkboxHtmlAttributes, string prefix = "")
        {
            StringBuilder output = new StringBuilder();
            string        text   = item.Text;

            if (text.Contains("-"))
            {
                text = text.Remove(text.IndexOf("-"));
            }

            output.Append("<div>");
            output.Append(prefix + "<label>");
            var checkboxList = new TagBuilder("input");

            checkboxList.MergeAttribute("type", "checkbox");
            checkboxList.MergeAttribute("name", name);
            checkboxList.MergeAttribute("value", item.Value);

            // Check to see if it’s checked
            if (item.Selected)
            {
                checkboxList.MergeAttribute("checked", "checked");
            }
            // Add any attributes
            if (checkboxHtmlAttributes != null)
            {
                checkboxList.MergeAttributes(checkboxHtmlAttributes);
            }

            checkboxList.SetInnerText(item.Text);
            output.Append(checkboxList.ToString(TagRenderMode.SelfClosing));
            output.Append("&nbsp; " + text + "</label>");
            //Verificando se há filhos
            foreach (CustomSelectListItem child in item.Children)
            {
                Dictionary <string, object> attributes = new Dictionary <string, object>();
                attributes.Add("relatedRoleName", item.Value);
                output.Append(CheckBox(helper, name, child, attributes, "&nbsp;&nbsp;&nbsp;&nbsp;"));
            }

            output.Append("</div>");
            return(MvcHtmlString.Create(output.ToString()));
        }