asp mvc中的复选框列表

时间:2010-10-05 14:18:54

标签: asp.net-mvc checkbox

我有3个表,项目,设备和项目设备(表2将多个关系链接起来)。设备连接到帐户,在帐户管理中,设备可以连接到项目。我想通过使用复选框来完成此操作。在创建或编辑项目时,应该有所有设备的列表以及选择它们的可能性。 换句话说,如何使用复选框填充项目设备表?

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

2 个答案:

答案 0 :(得分:3)

好吧,我不认为这会自动运行,因此您必须构建自己的网格,然后解析结果以重新保留它们。它实际上并不那么难。我在下面为你写了一个示例应用程序......

控制器:

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

}

设备型号:

  public class Device
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Project> Projects = new List<Project>();
    }

项目模型:

public class Project
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Device> Devices = new List<Device>();
}

LinkDeviceAndProject(查看模型):

public class LinkDeviceAndProject
{
    public List<Project> Projects = new List<Project>();
    public List<Device> Devices = new List<Device>();
}

查看:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<testchecks.Models.LinkDeviceAndProject>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Index</title>
</head>
<body>
    <% using (Html.BeginForm())
       { %>
    <div>
        <table>
            <tr>
                <td></td>
                <% foreach (var p in Model.Projects)
                   {%>
                   <td><%: p.Name%></td>
                <%} %>
            </tr>

            <% foreach (var d in Model.Devices)
               { %>
               <tr>
                <td><%: d.Name%></td>
                    <% foreach (var p in Model.Projects)
                       { %>
                       <td><%: Html.CheckBox("d-" + d.Id.ToString() + "_p-" + p.Id, d.Projects.Where(x => x.Id == p.Id).Count() > 0 ? true : false)%></td>
                    <%} %>
               </tr>
            <%} %>
        </table>
        <input type="submit" value="Save" />
    </div>
    <%} %>
</body>
</html>

答案 1 :(得分:1)

我没有尝试过,而且我对活动记录的了解有限,但我发现this which might be of use

HasAndBelongsToMany似乎就是你想要的。

希望这会有所帮助。如果我错过了这一点,请告诉我: - )