Asp.net mvc asp mvc中的复选框列表

Asp.net mvc asp mvc中的复选框列表,asp.net-mvc,checkbox,Asp.net Mvc,Checkbox,我有3个表,项目,设备和项目设备(表链接了多对多关系的2)。设备连接到帐户,在帐户管理中,设备可以连接到项目。我想通过使用复选框来实现这一点。创建或编辑项目时,应列出所有设备以及选择它们的可能性。 换句话说,如何使用复选框填充projectdevice表 我正在使用ActiveRecord访问我的数据我还没有尝试过,而且我对ActiveRecord的了解有限,但是我发现 hasandbelongtomany似乎就是你想要的 希望这有帮助。如果我没有抓住要点,请告诉我:-)我没有尝试过,而且我对活

我有3个表,项目,设备和项目设备(表链接了多对多关系的2)。设备连接到帐户,在帐户管理中,设备可以连接到项目。我想通过使用复选框来实现这一点。创建或编辑项目时,应列出所有设备以及选择它们的可能性。 换句话说,如何使用复选框填充projectdevice表


我正在使用ActiveRecord访问我的数据

我还没有尝试过,而且我对ActiveRecord的了解有限,但是我发现

hasandbelongtomany
似乎就是你想要的


希望这有帮助。如果我没有抓住要点,请告诉我:-)

我没有尝试过,而且我对活动记录的了解有限,但是我发现

hasandbelongtomany
似乎就是你想要的


希望这有帮助。如果我没有抓住要点,请告诉我:-)

好吧,我认为这不会自动起作用,因此您必须自己构建网格,然后解析结果以重新保存它们。其实没那么难。我在下面为您编写了一个示例应用程序

控制器:

public class HomeController : Controller
{

    [HttpGet]
    public ActionResult Index()
    {
        LinkDeviceAndProject Model = new LinkDeviceAndProject();
        Model.Devices = GetDevices();
        Model.Projects = GetProjects();
        return View(Model);
    }

    [HttpPost]
    public ActionResult Index(FormCollection SaveResults)
    {
        LinkDeviceAndProject Model = new LinkDeviceAndProject();
        Model.Devices = GetDevices();
        Model.Projects = GetProjects();

        foreach (var d in Model.Devices)
        {
            foreach (var p in Model.Projects)
            {
                string boxId = "d-" + d.Id.ToString() + "_p-" + p.Id.ToString();
                if (SaveResults[boxId] != null)
                {
                    string box = SaveResults[boxId];
                    bool boxValue = box == "true,false" ? true : false;
                    if (boxValue && d.Projects.Where(x => x.Id == p.Id).Count() == 0)
                        d.Projects.Add(p);
                    else if (!boxValue && d.Projects.Where(x => x.Id == p.Id).Count() > 0)
                        d.Projects.RemoveAll(x => x.Id == p.Id);
                }

            }
        }
        return View(Model);
    }

    private List<Device> GetDevices()
    {
        return new List<Device>() { new Device() { Id = 1, Name = "Device1" }, new Device() { Id = 2, Name = "Device2" } };
    }

    private List<Project> GetProjects()
    {
        return new List<Project>() { new Project() { Id = 1, Name = "Project1" }, new Project() { Id = 2, Name = "Project2" } };
    }

}
公共类HomeController:控制器
{
[HttpGet]
公共行动结果索引()
{
LinkDeviceAndProject模型=新的LinkDeviceAndProject();
Model.Devices=GetDevices();
Model.Projects=GetProjects();
返回视图(模型);
}
[HttpPost]
公共操作结果索引(FormCollection SaveResults)
{
LinkDeviceAndProject模型=新的LinkDeviceAndProject();
Model.Devices=GetDevices();
Model.Projects=GetProjects();
foreach(Model.Devices中的var d)
{
foreach(Model.Projects中的var p)
{
字符串boxId=“d-”+d.Id.ToString()+“_p-”+p.Id.ToString();
if(SaveResults[boxId]!=null)
{
字符串框=保存结果[boxId];
bool-boxValue=box==“真,假”?真:假;
if(boxValue&&d.Projects.Where(x=>x.Id==p.Id).Count()==0)
d、 增加(p);
else if(!boxValue&&d.Projects.Where(x=>x.Id==p.Id).Count()>0)
d、 Projects.RemoveAll(x=>x.Id==p.Id);
}
}
}
返回视图(模型);
}
私有列表GetDevices()
{
返回new List(){new Device(){Id=1,Name=“Device1”},new Device(){Id=2,Name=“Device2”};
}
私有列表GetProjects()
{
返回new List(){new Project(){Id=1,Name=“Project1”},new Project(){Id=2,Name=“Project2”};
}
}
设备型号:

  public class Device
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Project> Projects = new List<Project>();
    }
公共类设备
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表项目=新列表();
}
项目模型:

public class Project
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Device> Devices = new List<Device>();
}
公共类项目
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表设备=新列表();
}
LinkDeviceAndProject(视图模型):

公共类链接设备和项目
{
公共列表项目=新列表();
公共列表设备=新列表();
}
视图:


指数
x、 Id==p.Id).Count()>0?正确:错误)%>

好吧,我认为这不会自动起作用,因此您必须自己构建网格,然后解析结果以重新保存它们。其实没那么难。我在下面为您编写了一个示例应用程序

控制器:

public class HomeController : Controller
{

    [HttpGet]
    public ActionResult Index()
    {
        LinkDeviceAndProject Model = new LinkDeviceAndProject();
        Model.Devices = GetDevices();
        Model.Projects = GetProjects();
        return View(Model);
    }

    [HttpPost]
    public ActionResult Index(FormCollection SaveResults)
    {
        LinkDeviceAndProject Model = new LinkDeviceAndProject();
        Model.Devices = GetDevices();
        Model.Projects = GetProjects();

        foreach (var d in Model.Devices)
        {
            foreach (var p in Model.Projects)
            {
                string boxId = "d-" + d.Id.ToString() + "_p-" + p.Id.ToString();
                if (SaveResults[boxId] != null)
                {
                    string box = SaveResults[boxId];
                    bool boxValue = box == "true,false" ? true : false;
                    if (boxValue && d.Projects.Where(x => x.Id == p.Id).Count() == 0)
                        d.Projects.Add(p);
                    else if (!boxValue && d.Projects.Where(x => x.Id == p.Id).Count() > 0)
                        d.Projects.RemoveAll(x => x.Id == p.Id);
                }

            }
        }
        return View(Model);
    }

    private List<Device> GetDevices()
    {
        return new List<Device>() { new Device() { Id = 1, Name = "Device1" }, new Device() { Id = 2, Name = "Device2" } };
    }

    private List<Project> GetProjects()
    {
        return new List<Project>() { new Project() { Id = 1, Name = "Project1" }, new Project() { Id = 2, Name = "Project2" } };
    }

}
公共类HomeController:控制器
{
[HttpGet]
公共行动结果索引()
{
LinkDeviceAndProject模型=新的LinkDeviceAndProject();
Model.Devices=GetDevices();
Model.Projects=GetProjects();
返回视图(模型);
}
[HttpPost]
公共操作结果索引(FormCollection SaveResults)
{
LinkDeviceAndProject模型=新的LinkDeviceAndProject();
Model.Devices=GetDevices();
Model.Projects=GetProjects();
foreach(Model.Devices中的var d)
{
foreach(Model.Projects中的var p)
{
字符串boxId=“d-”+d.Id.ToString()+“_p-”+p.Id.ToString();
if(SaveResults[boxId]!=null)
{
字符串框=保存结果[boxId];
bool-boxValue=box==“真,假”?真:假;
if(boxValue&&d.Projects.Where(x=>x.Id==p.Id).Count()==0)
d、 增加(p);
else if(!boxValue&&d.Projects.Where(x=>x.Id==p.Id).Count()>0)
d、 Projects.RemoveAll(x=>x.Id==p.Id);
}
}
}
返回视图(模型);
}
私有列表GetDevices()
{
返回new List(){new Device(){Id=1,Name=“Device1”},new Device(){Id=2,Name=“Device2”};
}
私有列表GetProjects()
{
返回new List(){new Project(){Id=1,Name=“Project1”},new Project(){Id=2,Name=“Project2”};
}
}
设备型号:

  public class Device
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Project> Projects = new List<Project>();
    }
公共类设备
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表项目=新列表();
}
项目模型:

public class Project
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Device> Devices = new List<Device>();
}
公共类项目
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表设备=新列表();
}
LinkDeviceAndProject(视图模型):

公共类链接设备和项目
{
公共列表项目=新列表();
平民的