C# MVC3中的CheckboxList事件

C# MVC3中的CheckboxList事件,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我正在为MVC3中的通信类型创建一个复选框列表。我的问题是,当我单击复选框时,它只是在选择,但我想单击文本,它也是要选择的 我的控制器代码: IEnumerable<M_TblCommunicationType> communicationlList = (from x in db.M_TblCommunicationType select x).ToList(); ViewBag.communication = communicationlList.Select(c =

我正在为MVC3中的通信类型创建一个复选框列表。我的问题是,当我单击复选框时,它只是在选择,但我想单击文本,它也是要选择的

我的控制器代码:

   IEnumerable<M_TblCommunicationType> communicationlList = (from x in db.M_TblCommunicationType select x).ToList();
   ViewBag.communication = communicationlList.Select(c => new SelectListItem
   {
      Text = c.Communication.ToString(),
      Value = c.Communication_Type.ToString()
   });    
IEnumerable communicationList=(从db.M_TblCommunicationType中的x选择x.ToList();
ViewBag.communication=通信列表。选择(c=>new SelectListItem
{
Text=c.Communication.ToString(),
Value=c.Communication\u Type.ToString()
});    
我的视图代码:

@Html.LabelFor(model => model.Communication_Type)<span class="mandatory">
                * </span></li>
            <li class="input">
                <div class="other">
                    @{IEnumerable<SelectListItem> comminicationType = ViewBag.communication;
                      foreach (var item in comminicationType)
                      { 
                        <input type="checkbox" name="Communication_Typeids" value="@item.Value" id='communicationtype'/>
                        <label for="@item.Value">@item.Text</label>
  @Html.ValidationMessageFor(model => model.Communication_Type)
@Html.LabelFor(model=>model.Communication\u类型)
*
  • @{IEnumerable comminicationType=ViewBag.communication; foreach(佣金类型中的var项目) { @项目.案文 @Html.ValidationMessageFor(model=>model.Communication\u类型)
    标签的
  • 属性应与要绑定的元素的id一起使用

    试试这个:

    <label for="communicationtype">@item.Text</label>
    
    @item.Text
    

    希望这有帮助。

    是的,但它只对一个复选框起作用,但我有四个复选框。无论文本是什么,我都选中了第一个复选框,只选择@item.text。我尝试过这样做,但不起作用!!!!你在
    ViewBag.communication@(item)中得到了什么
    .Try
    @ViewBag.communication(@item)
    查看包是否只返回id的单个值?@ViewBag.communication(@item)我尝试过,但出现类似“无法调用非委托类型”的错误