1

我正在尝试在视图中显示多组带有标题复选框的复选框,如下图所示。 在此处输入图像描述

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

    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

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

控制器

    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);
        }

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

4

1 回答 1

4

创建类:

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。

于 2013-07-08T13:20:03.497 回答