我有3个表,项目,设备和项目设备(链接2表达多对多关系)。一个设备连接到一个账户,在账户管理中,设备可以连接到一个项目。我想通过使用复选框来做到这一点。在创建或编辑项目时,应该有所有设备的列表以及选择它们的可能性。 换句话说,我如何使用复选框来填充projectdevice表?在asp mvc中的复选框列表
我使用ActiveRecord来访问我的数据
我有3个表,项目,设备和项目设备(链接2表达多对多关系)。一个设备连接到一个账户,在账户管理中,设备可以连接到一个项目。我想通过使用复选框来做到这一点。在创建或编辑项目时,应该有所有设备的列表以及选择它们的可能性。 换句话说,我如何使用复选框来填充projectdevice表?在asp mvc中的复选框列表
我使用ActiveRecord来访问我的数据
嗯,我不认为这会自动工作,所以你将不得不建立自己的网格,然后解析结果重新坚持他们。其实并不那么难。我写了一个示例应用程序下面为您...
控制器:
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>
我没有尝试过了,我的活动记录的知识是有限的,但是我发现this which might be of use
HasAndBelongsToMany
似乎是你想要的。
希望这会有所帮助。如果我错过了这一点,然后让我知道:-)
什么方法的数据访问您使用的? ADO.Net? ORM?因为这取决于你如何实现链接。有些ORM会为你做。 – WestDiscGolf 2010-10-05 14:20:30
更新了我的问题 – Prd 2010-10-05 14:22:03