我有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