views:

38

answers:

2

Ive got 3 tables, projects, devices and projectdevice (table that links the 2 for a many to many relation). A device is connected to an account and in the account management the device can be connected to a project. I want to do this by using checkboxes. When creating or editing a project there should be a list of all devices and the possibility to select them. In other words, how do I fill the projectdevice table by using checkboxes?

I am using ActiveRecord to access my data

+1  A: 

I've not tried it and my knowledge of active record is limited, however I've found this which might be of use

HasAndBelongsToMany seem to be what you want.

Hope this helps. If I've missed the point then let me know :-)

WestDiscGolf
+1  A: 

Well, I dont think this will automatically work, so you will have to build out the grid your self and then parse the results to re-persist them. Its not actually that hard. I have written a sample app for you below...

Controller:

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

}

Device Model:

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

Project Model:

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

LinkDeviceAndProject (View Model):

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

View:

<%@ 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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>
CrazyDart