将 Model.ID 绑定到复选框列表,将 post Model.X、Model.Y 等属性绑定到控制器
Binding Model.ID to Checkbox list and post Model.X, Model.Y, etc. properties to Controller
在一个 MVC
应用程序中,我加入了多个表并将其从 Controller
返回到 View
,如下所示:
| EmployeeID | ControlID | DoorAddress | DoorID | DoorName |
------------------------------------------------------------
| 921 | 1 | 1 | 101 | Door 1 |
| 921 | 1 | 2 | 102 | Door 2 |
| 921 | 1 | 3 | 103 | Door 3 |
| 921 | 1 | 4 | 104 | Door 4 |
------------------------------------------------------------
控制器:
public ActionResult Edit(int? id)
{
// Create and execute raw SQL query.
string query = "SELECT a.EmployeeID, a.ControlID, a.DoorAddress, t.DoorID, t.DoorName FROM TEmpAccess AS a " +
"INNER JOIN TDoor AS t ON a.ControlID = t.ControlID and a.DoorAddress = t.DoorAddress where EmployeeID = " + id.ToString();
IEnumerable<EmpAccessViewModel> data = db.Database.SqlQuery<EmpAccessViewModel>(query);
return View(data.ToList());
}
我想将 DoorName
值(门 1、门 2、门 3、门 4)绑定到 checkbox list
并让用户 select 它们。之后,我想将 select 门的相应 EmployeeID
、ControlID
、DoorAddress
、DoorID
值传递给 Controller
。例如,如果用户 selects Door 1 和 Door 3,那么我会将下面的这些值传递给 Controller
:
| EmployeeID | ControlID | DoorAddress | DoorID |
-------------------------------------------------
| 921 | 1 | 1 | 101 |
| 921 | 1 | 3 | 103 |
-------------------------------------------------
通过在视图中使用 razor
语法或 javascript
,我该怎么做?提前致谢。
如果你想在要传递给控制器的视图中 Table 的开头有一个复选框,并且每一行都由 DoorID
唯一标识,你可以尝试这样的事情.
在视图中
<table>
@foreach (var item in Model)
{
<tr>
<td><input type="checkbox" value="@item.DoorID" name="doorid" /></td>
<td>@item.EmployeeID</td>
<td>@item.ControlID</td>
<td>@item.DoorName</td>
</tr>
}
</table>
在控制器中
public void ControllerName(int[] doorid)
{
foreach(var item in doorid)
//do something
}
在一个 MVC
应用程序中,我加入了多个表并将其从 Controller
返回到 View
,如下所示:
| EmployeeID | ControlID | DoorAddress | DoorID | DoorName |
------------------------------------------------------------
| 921 | 1 | 1 | 101 | Door 1 |
| 921 | 1 | 2 | 102 | Door 2 |
| 921 | 1 | 3 | 103 | Door 3 |
| 921 | 1 | 4 | 104 | Door 4 |
------------------------------------------------------------
控制器:
public ActionResult Edit(int? id)
{
// Create and execute raw SQL query.
string query = "SELECT a.EmployeeID, a.ControlID, a.DoorAddress, t.DoorID, t.DoorName FROM TEmpAccess AS a " +
"INNER JOIN TDoor AS t ON a.ControlID = t.ControlID and a.DoorAddress = t.DoorAddress where EmployeeID = " + id.ToString();
IEnumerable<EmpAccessViewModel> data = db.Database.SqlQuery<EmpAccessViewModel>(query);
return View(data.ToList());
}
我想将 DoorName
值(门 1、门 2、门 3、门 4)绑定到 checkbox list
并让用户 select 它们。之后,我想将 select 门的相应 EmployeeID
、ControlID
、DoorAddress
、DoorID
值传递给 Controller
。例如,如果用户 selects Door 1 和 Door 3,那么我会将下面的这些值传递给 Controller
:
| EmployeeID | ControlID | DoorAddress | DoorID |
-------------------------------------------------
| 921 | 1 | 1 | 101 |
| 921 | 1 | 3 | 103 |
-------------------------------------------------
通过在视图中使用 razor
语法或 javascript
,我该怎么做?提前致谢。
如果你想在要传递给控制器的视图中 Table 的开头有一个复选框,并且每一行都由 DoorID
唯一标识,你可以尝试这样的事情.
在视图中
<table>
@foreach (var item in Model)
{
<tr>
<td><input type="checkbox" value="@item.DoorID" name="doorid" /></td>
<td>@item.EmployeeID</td>
<td>@item.ControlID</td>
<td>@item.DoorName</td>
</tr>
}
</table>
在控制器中
public void ControllerName(int[] doorid)
{
foreach(var item in doorid)
//do something
}