c# - 将复选框列表绑定(bind)到数据模型中的字典

标签 c# asp.net-mvc-3 data-binding checkbox model

我是 MVC3 的新手,我不明白如何使用复选框。 在我的模型中,我有一个服务列表和一个可用服务列表。 这个想法是显示所有可用的服务,并让用户选择一次使用 ba 选中相应的复选框。

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

我能够使用 Controller 的私有(private)方法正确加载服务表 私有(private)字典 AssignServiceToActivity(事件事件) {

            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

最佳答案

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

关于c# - 将复选框列表绑定(bind)到数据模型中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16111694/

相关文章:

c# - Safari Mac OSX 退格问题

c# - 设计模式建议 - 将 ViewModel 与域模型分离

asp.net-mvc-3 - 模型中的公共(public)方法在 Razor View 上不可见

c# - WPF 数据绑定(bind) - 中间对象的 Getter/Setter 未命中

entity-framework - 将 Entity Framework 实体公开为 ViewModel 上的属性以进行 MVVM 数据绑定(bind)

wpf - 使用comboBox作为源将自动生成的DataGrid分组

c# - 我们可以在 Window Forms (.Net) 应用程序中使用 Google Ads 吗?

c# - 在 WatiN 中创建一个 Chrome 实例

javascript - 使用 jquery 检查和取消选中具有动态 id 和类的复选框

c# - 如何以编程方式覆盖构建和启动操作?