C# MVCCheckBox列表满足1对多关系

C# MVCCheckBox列表满足1对多关系,c#,asp.net-mvc-5,models,C#,Asp.net Mvc 5,Models,我有一个与另一个类有1对多关系的类。为此,我将使用一流的汽车和一流的齿轮。我需要创建一个表单,用于注册一辆汽车,用户需要指定一个档位选择 public class Car { public int id { get; set; } public string desc { get; set; } public List<Gear> Gears { get; set; } } public class Gear { public int gid { get; s

我有一个与另一个类有1对多关系的类。为此,我将使用一流的汽车和一流的齿轮。我需要创建一个表单,用于注册一辆汽车,用户需要指定一个档位选择

public class Car
{
   public int id { get; set; }
   public string desc { get; set; }
   public List<Gear> Gears { get; set; }
}
public class Gear
{
   public int gid { get; set; }
   public int gname { get; set; }
}
控制器看起来像:

从数据库上下文中获取的齿轮将 “齿轮”、“齿轮1”、“齿轮2”、“齿轮3”、“齿轮4”、“齿轮5”、“齿轮6”、“齿轮7”

公共行动创建() { ViewBag.Gears=新的选择列表(db.Gears,“gid”,“gname”); 列表_gears=新列表(); foreach(查看包中的G档位。档位) { _添加(新的GearViewModel(G,false)); } ViewBag.GearsCheckList=_gears.ToList(); 返回视图(); } 现在,这就是我要解决的问题,就是如何在CreateView中显示和捕获细节


我需要有关如何设计创建表单以及如何捕获信息的帮助

首先,视图模型在编辑时不应包含数据模型。您应该查看模型(根据需要添加验证和显示属性)

然后,视图将被删除

@model CarVM
....
@using (Html.BeginForm())
{
    ..... // elements for editing ID and Description properties of CarVM
    @for (int i = ; i < Model.Gears.Count; i++)
    {
        <div>
            @Html.HiddenFor(m => m.Gears[i].ID)
            @Html.HiddenFor(m => m.Gears[i].Name) // include if your want to get this in the POST method as well
            @Html.CheckboxFor(m => m.Gears[i].IsSelected)
            @Html.LabelFor(m => m.Gears.IsSelected, Model.Gears[i].Name)
        </div>
    }
    <input type="submit" .... />
}
@model-CarVM
....
@使用(Html.BeginForm())
{
..…//用于编辑CarVM的ID和描述属性的元素
@for(int i=;im.Gears[i].ID)
@Html.HiddenFor(m=>m.Gears[i].Name)//如果您想在POST方法中也得到它,请包括
@Html.CheckboxFor(m=>m.Gears[i].IsSelected)
@LabelFor(m=>m.Gears.IsSelected,Model.Gears[i].Name)
}
}
然后在POST方法中

public ActionResult Create(CarVM model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    // To get the ID's of the selected gears
    IEnumerable<int> selected = model.Gears.Where(x => x.IsSelected).Select(x => x.ID);
    // Initialize your data models, save and redirect
}
public ActionResult创建(CarVM模型)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
//获取所选档位的ID
IEnumerable selected=model.Gears.Where(x=>x.IsSelected)。Select(x=>x.ID);
//初始化数据模型,保存并重定向
}
public class CarVM
{
    public int? ID { get; set; }
    public string Description { get; set; }
    public List<GearVM> Gears { get; set; }
}
public class GearVM
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsSelected { get; set; }
}
public ActionResult Create()
{
    var gears = db.Gears;
    CarVM model = new CarVM
    {
        Gears = gears.Select(x => new GearVM
        {
            ID = x.gid,
            Name = x.gname
        }).ToList()
    };
    return View(model);
}
@model CarVM
....
@using (Html.BeginForm())
{
    ..... // elements for editing ID and Description properties of CarVM
    @for (int i = ; i < Model.Gears.Count; i++)
    {
        <div>
            @Html.HiddenFor(m => m.Gears[i].ID)
            @Html.HiddenFor(m => m.Gears[i].Name) // include if your want to get this in the POST method as well
            @Html.CheckboxFor(m => m.Gears[i].IsSelected)
            @Html.LabelFor(m => m.Gears.IsSelected, Model.Gears[i].Name)
        </div>
    }
    <input type="submit" .... />
}
public ActionResult Create(CarVM model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    // To get the ID's of the selected gears
    IEnumerable<int> selected = model.Gears.Where(x => x.IsSelected).Select(x => x.ID);
    // Initialize your data models, save and redirect
}