Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将复选框列表绑定到datamodel中的字典_C#_Asp.net Mvc 3_Data Binding_Checkbox_Model - Fatal编程技术网

C# 将复选框列表绑定到datamodel中的字典

C# 将复选框列表绑定到datamodel中的字典,c#,asp.net-mvc-3,data-binding,checkbox,model,C#,Asp.net Mvc 3,Data Binding,Checkbox,Model,我是MVC3新手,不知道如何使用复选框。 在我的模型中,我有一个服务列表和一个可用服务列表。 其思想是显示所有可用的服务,并让用户选中相应的复选框,选择要使用的服务 public Activity() { ..... this.Services = new List<Service>(); this.ServicesChecklist = new Dictionary<Service, bo

我是MVC3新手,不知道如何使用复选框。 在我的模型中,我有一个服务列表和一个可用服务列表。 其思想是显示所有可用的服务,并让用户选中相应的复选框,选择要使用的服务

public Activity()
        {
            .....
            this.Services = new List<Service>();
            this.ServicesChecklist = new Dictionary<Service, bool>();
        }

        public virtual ICollection<Service> Services { get; set; }
        public Dictionary<Service, bool> ServicesChecklist { get; set; }

您需要使用带有“提交”按钮的表单将更改提交回服务器,或者在发生更改事件时使用JavaScript将三个更改提交回服务器。

请参阅此页,它将为您的问题提供答案,并提供发生更改的背景。
            List<Service> s = new List<Service>(db.Services);
            Dictionary<Service, bool> ServicesChecklist = new Dictionary<Service, bool>();
            bool found;


            foreach (var ser in s)
            {
                found = false;
                foreach (var currService in activity.Services)
                {
                    if (ser.id == currService.id)
                    {
                        ServicesChecklist.Add(ser, true);
                        found = true;
                        break;
                    }
                }
                if (!found)
                    ServicesChecklist.Add(ser, false);
            }
            return ServicesChecklist;
        }


in my view I am trying to bind the checkboxes to the ServicesChecklist dictionary

<div id="tabs-2">
         <table style="width:100%">
            <tr>
               <th>
                  Service
               </th>
               <th>
                  Type
               </th>
               <th>
                  Contact
               </th>
               <th>
                  Phone
               </th>
            </tr>
            @foreach (var s in @Model.ServicesChecklist)
            {
            <tr>
               <td>

                  @Html.CheckBox(s.Key.name, s.Key.id) 
                  @Html.DisplayFor(modelItem => s.Key.name)                  
                </td>
               <td>
                  @Html.DisplayFor(modelItem => s.Key.ServiceType.name)
               </td>
               <td>
                  @Html.DisplayFor(modelItem => s.Key.contact_name)
               </td>
               <td>
                  @Html.DisplayFor(modelItem => s.Key.contact_phone)
               </td>

            </tr>
            }
         </table>
      </div>
![enter image description here][1]


  [1]: http://i.stack.imgur.com/Buklf.png