如何从Edit View MVC中的复选框的控制器值中获取数据?

时间:2018-07-31 07:57:13

标签: c# asp.net asp.net-mvc

  

需要在编辑视图ViewModel中绑定复选框列表和数据

public void GetCustomerTypeViewModel(IEnumerable<CustomerTypeViewModel> selected, IEnumerable<CustomerTypeViewModel> all)
        {
        foreach (var item in all)
        {
            GetCustomerType.Add(item);
        }

        foreach (var item in selected)
        {
            GetCustomerType.FirstOrDefault(x => x.Description == item.Description).Selected = true;
        }
    }       
  

CustomerTypeManager

public CustomerTypeViewModel GetCustomerType(int? customerId)
        {
            var query = "SELECT * FROM CustomerCheckedType where customerId = @customerId";
            return context.GetObject<CustomerTypeViewModel>(query, new { Id = customerId });
        }
  

控制器

 public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var customer = customerManager.Get(id);      
            if (customer == null)
            {
                return HttpNotFound();
            }

            var vm = new CustomerViewModel();
            vm.GetCustomerTypeViewModel(new List<CustomerTypeViewModel>(), customerTypeManager.GetAll());
            return View(vm);
        }
  

编辑视图

 <div class="form-group">
                            @{
                                for (int i = 0; i < Model.GetCustomerType.Count(); i++)
                                {
                                    <div class="col-md-10">
                                        @Html.Label(Model.GetCustomerType[i].Description, new { @class = "control-label col-md-2" })
                                        @Html.CheckBoxFor(model => model.GetCustomerType[i].Selected)
                                        @Html.HiddenFor(model => model.GetCustomerType[i].Id, new { data_val = false })
                                        @Html.HiddenFor(model => model.GetCustomerType[i].Description, new { data_val = false })
                                    </div>
                                }
                            }
                        </div>
  

编辑视图绑定复选框,但不返回我在其中创建的数据   创建表单..因此在编辑中所有内容都为空白。

0 个答案:

没有答案