C# 如何创建从数据库中存储的列表中获取复选框的编辑视图?

C# 如何创建从数据库中存储的列表中获取复选框的编辑视图?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,需要使用createView中选定的复选框创建编辑视图 public class CustomerTypeViewModel { [Required] public int? Id { get; set; } public string Description { get; set; } [Display(Name = "Please select the Type")] public bool Selected { get; set; } } Cust

需要使用createView中选定的复选框创建编辑视图

public class CustomerTypeViewModel
{
    [Required]
    public int? Id { get; set; }
    public string Description { get; set; }
    [Display(Name = "Please select the Type")]
    public bool Selected { get; set; }
}
CustomerView何时加载CustomerTypeViewModel列表

public List<CustomerTypeViewModel> SelectedCustomerTypes { get; set; }
private List<CustomerTypeViewModel> selectionList = new List<CustomerTypeViewModel>();
public CustomerViewModel()
{
    SelectedCustomerTypes = new List<CustomerTypeViewModel>();
}

public void SetCustomerTypeViewModel(IEnumerable<CustomerTypeViewModel> selected, IEnumerable<CustomerTypeViewModel> all)
{
    SelectedCustomerTypes.Clear();
    foreach (var item in all)
    {
        SelectedCustomerTypes.Add(item);
    }
    foreach (var item in selected)
    {
        SelectedCustomerTypes.FirstOrDefault(x => x.Description == item.Description).Selected = true;
    }
}

public List<CustomerTypeViewModel> GetTipi()
{
    return selectionList;
}
public List SelectedCustomerTypes{get;set;}
私有列表selectionList=新建列表();
公共CustomerServiceModel()
{
SelectedCustomerTypes=新列表();
}
public void SetCustomerTypeViewModel(选中IEnumerable,全部IEnumerable)
{
SelectedCustomerTypes.Clear();
foreach(所有var项目)
{
选择的客户类型。添加(项目);
}
foreach(所选中的var项目)
{
SelectedCustomerTypes.FirstOrDefault(x=>x.Description==item.Description)。Selected=true;
}
}
公共列表GetTipi()
{
返回选择列表;
}
在控制器中,我应该调用一个从manager获取customertypes的方法

public CustomerTypeViewModel GetCustomerType(int? customerId)
{
    var query = "SELECT * FROM CustomerType where Id = @Id";
    return context.GetObject<CustomerTypeViewModel>(query, new { Id = customerId });
}
公共CustomerTypeViewModel GetCustomerType(int?customerId) { var query=“从CustomerType中选择*,其中Id=@Id”; 返回context.GetObject(查询,new{Id=customerId}); } 现在,在控制器上进行编辑

[AuthorizeRoles(RoleNames.CanEditCustomer)]
public ActionResult Edit(int? id, int? customerId)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var customer = customerManager.Get(customerId);
    var vm = new CustomerViewModel();
    vm.SetCustomerTypeViewModel(new List<CustomerTypeViewModel>(), customerTypeManager.GetAll());
    if (customer == null)
    {
        return HttpNotFound();
    }
    return View(customer);
}
[授权角色(RoleNames.CanEditCustomer)]
公共操作结果编辑(int?id,int?customerId)
{
if(id==null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var customer=customerManager.Get(customerId);
var vm=new CustomerViewModel();
SetCustomerTypeViewModel(新列表(),customerTypeManager.GetAll());
如果(客户==null)
{
返回HttpNotFound();
}
返回视图(客户);
}
我被告知在manager GetCustomerType(customerId)中创建一个方法 model.SelectedCustomerTypes[i]。选中,新建{@checked=“checked”}) @Html.HiddenFor(model=>model.SelectedCustomerTypes[i].Id,new{data\u val=false}) @Html.HiddenFor(model=>model.SelectedCustomerTypes[i].Description,new{data\u val=false}) } }
您需要修复控制器中的GetCustomerType方法。试试这个:

public IActionResult GetCostumerType(int? id)
{

   if (id == null)
   {
      return NotFound();
   }

   var test = from custom in _yourContext.CustomerType                   
              .Where(p => p.CustomerType.Id == customerId)
              select custom;
              //change _yourContext with your context variable                  

   if (test == null)
   {
     return NotFound();
   }

   return View(test);
}

通过这种方式,您将获得与方法中传递的id相关联的自定义类型。

执行以下代码:“var query=“SELECT*FROM CustomerType,其中id=@id”;”在控制器中工作?是的,它获取已创建复选框的Id。CustomerType具有Id、说明和布尔值。布尔值为true时,该值被发送到新表CustomerCheckedType.。该代码不起任何作用。您认为您正在获取ID,但如果调试它,您将看到您至少没有像下面这样连接字符串:var query=“SELECT*FROM CustomerType where ID=”+@ID;但它仍然会给出一个错误,因为编译器不知道@Idany的概念是什么,我应该如何修复它?“创建用于”复选框效果良好。但无法对编辑视图进行编辑我将此添加到CustomerController?这将允许您获取与要传递的id关联的CustomerType更改GetCustomerType方法
public IActionResult GetCostumerType(int? id)
{

   if (id == null)
   {
      return NotFound();
   }

   var test = from custom in _yourContext.CustomerType                   
              .Where(p => p.CustomerType.Id == customerId)
              select custom;
              //change _yourContext with your context variable                  

   if (test == null)
   {
     return NotFound();
   }

   return View(test);
}