显示Viewbag列表项的计数

时间:2016-10-17 13:42:59

标签: c# asp.net-mvc razor

您好有办法显示一个视图包列表并显示计数

喜欢:

  

阿拉伯联合酋长国(8)
  安哥拉(4)
  阿根廷(7)
  奥地利(0)
  比利时(11)

目前我在视图中有一个Checkboxlist(服装助手)

@Html.CheckBoxList("checkedLocation", (ViewBag.country_list as MultiSelectList))

仅显示国家/地区。如果相同的列表有重要的话,那就太好了 基本上我有一个公司表与国家领域。只是想表明在巴西有十家公司可供选择,如巴西(10)

这是如何填充viwbag

List<country> listInfo2 = db.countries.Where(row => row.covered == 1).ToList();
List<string> checkedValues = new List<string>();
MultiSelectList checkedValuesList = new MultiSelectList(listInfo2, "country_name", "country_name", checkedValues);
ViewBag.country_list = checkedValuesList

2 个答案:

答案 0 :(得分:1)

您可以返回新的动态对象列表。这是一个例子:

public class country
{
    public string Name { get; set; }
    public string Covered { get; set; }
}
List<country> contries = new List<country>() { 
    new country(){ Name = "Siraj", Covered = "1"},
    new country(){ Name = "Kumail", Covered = "1"},
    new country(){ Name = "Ali", Covered = "1"},
    new country(){ Name = "Haider", Covered = "1"}
};
 var query = (from x in contries
              group x.Id by x.Name into g
              select new { CustomName = string.Format("{0} ({1})", g.Key, g.Count()) }).ToList();

答案 1 :(得分:0)

最后做对了 控制器:

//COUNTRIES TO CHECKBOX LIST
        List<DeviceGroupViewModel> listInfo2 = db.companies.GroupBy(fu => fu.COM_COUNTRY).Select(g => new DeviceGroupViewModel { Type = g.Key, Count = g.Count() }).ToList();
        MultiSelectList checkedValuesList = new MultiSelectList(listInfo2, "Type", "Count");
        ViewBag.country_list = checkedValuesList;

查看:

 @Html.CheckBoxList2("checkedLocation", (ViewBag.country_list as MultiSelectList))

Checkboxlist Helper:

 public static MvcHtmlString CheckBoxList2(this HtmlHelper htmlHelper, string name, MultiSelectList listInfos)
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentException("This parameter cannot be null or empty!", "name");

        }
        if (listInfos == null)
        {
            throw new ArgumentException("This parameter cannot be null!", "listInfos");
        }


        //if (listInfos.Count<SelectListItem>() < 1)
        //{
        //    throw new ArgumentException("The count must be greater than 0!", "listInfos");
        //}

        List<string> selectedValues = (List<string>)listInfos.SelectedValues;
        StringBuilder sb = new StringBuilder();



        foreach (SelectListItem info in listInfos)
        {

            sb.Append("<li class='Navlist'>");
            TagBuilder builder = new TagBuilder("input");

            if (info.Selected) builder.MergeAttribute("checked", "checked");

           //builder.MergeAttributes<string, object>(htmlAttributes);

            builder.MergeAttribute("type", "checkbox");

            builder.MergeAttribute("value", info.Value);

            builder.MergeAttribute("name", name);

            builder.MergeAttribute("id", info.Value);

            builder.MergeAttribute("class", "filter-classif");



            builder.InnerHtml = "<label>" + info.Value + "(" + info.Text + ")</label>";



            sb.Append(builder.ToString(TagRenderMode.Normal));
            sb.Append("</li>");



        }