Skip to main content
 首页 » 编程设计

c#之MVC4 : How to show Group of checkboxes with header checkbox

2024年02月13日13lovecherry

我正在尝试显示多组带有标题复选框的复选框,如下图所示。

从数据库中我得到这样的数据,

    CODE     SUBCODE       DESCR             DESCR              PrevOptIn 
    GLOBAL   BOOKS         Books             Global products    True 
    GLOBAL   ENERGY4ME     Energy            Global products    True 
    GLOBAL   JOURNALS      Journals          Global products    True 
    REGION   ASIA_PACIFIC  Asia Pacific      Geographical       False 
    REGION   CANADA        Canada            Geographical       True 
    REGION   EUROPE        Europe            Geographical       True 
    SPECIAL  SALES         Promotional       Notices            False 
    REGIONAL PROGRAMS      Conferences       Events             False 
    REGIONAL COURSES       Training courses  Events             False 
    REGIONAL EVENTS        Web events        Events             True 

当我单击标题复选框下方的标题复选框时,该复选框组应选中/取消选中。

Controller

    public ActionResult Index(string cusId = null,  
        string emailId = null, string marketoId=null ) 
            { 
 
    _custCommPref.CustomerId = cusId; 
    _custCommPref.MarketId = marketId; 
 
    _lstOptInInterest = new LinkedList<OptInInterestArea> 
        ((from a in _dbEntitiesA.APP_SUBCODE.Where(x => x.TYPE == "OPT_IN" && 
 x.ACTIVE_FLAG == "Y") 
          join appCode in _personifyEntities.APP_CODE 
          on new { CODE = a.CODE, TYPE = a.TYPE } equals new {  
                   CODE = appCode.CODE, TYPE = appCode.TYPE } 
          select new OptInInterestArea() 
          { 
               Code = a.CODE, 
               SubCode = a.SUBCODE, 
               SubCodeDescription = a.DESCR, 
               CodeDescription = appCode.DESCR 
         }).ToList()); 
 
         _lstOptInInterest = new LinkedList<OptInInterestArea> 
          ( 
              (from a in _lstOptInInterest 
                join b in _dbEntitiesB.CustCommPreferences 
                on new { CODE = a.Code, SUBCODE = a.SubCode } equals new {  
                         CODE = b.Code, SUBCODE = b.Subcode } 
                into leftGroup 
                from b in leftGroup.DefaultIfEmpty() 
                select new OptInInterestArea() 
                { 
                  Code = a.Code, 
                  SubCode = a.SubCode, 
                  SubCodeDescription = a.SubCodeDescription, 
                  CodeDescription = a.CodeDescription, 
                  PrevOptIn = b != null && b.OptedIn == true 
               }).ToList() 
            ); 
 
            _custCommPref.OptInInterestAreas =  
                    new List<OptInInterestArea>(_lstOptInInterest); 
            return View(_custCommPref); 
        } 

我在这里停下来创造更远的视野。请建议我。

请您参考如下方法:

创建类:

class CheckBoxGroup 
{ 
    public string Name {get;set;} 
    public bool IsChecked {get;set;} 
    public List<CheckBoxItem> {get;set;} 
} 
 
class CheckBoxItem 
{ 
    public string Name {get;set;} 
    public bool IsChecked {get;set;} 
} 

将属性包含到通用页面模型中:

public List<CheckBoxGroup> CheckboxGroups 

为类型 CheckBoxGroup 和 CheckBoxItem 实现 EditorTemplate。只需调用:

@Html.EditorFor(model => model.CheckBoxGroups) 

如果您想要某种客户端行为,例如当父级检查所有子级时也进行检查 - 您将需要编写一些 JavaScript。