Что ж, я не думаю, что это сработает автоматически, поэтому вам придется выстраивать сетку самостоятельно, а затем анализировать результаты, чтобы сохранить их.На самом деле это не так сложно.Я написал пример приложения для вас ниже ...
Контроллер:
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>