gpt4 book ai didi

javascript - 将 Model.ID 绑定(bind)到复选框列表并将 Model.X、Model.Y 等属性发布到 Controller

转载 作者:行者123 更新时间:2023-12-03 07:43:26 30 4
gpt4 key购买 nike

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

Controller :

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)绑定(bind)到复选框列表,并让用户选择它们。之后,我想将所选门的相应 EmployeeIDControlIDDoorAddressDoorID 值传递给 Controller 。例如,如果用户选择门 1 和门 3,那么我会将下面的这些值传递给 Controller ​​:

| EmployeeID | ControlID | DoorAddress | DoorID |
-------------------------------------------------
| 921 | 1 | 1 | 101 |
| 921 | 1 | 3 | 103 |
-------------------------------------------------

通过在 View 中使用 razor 语法或 javascript,我该如何做到这一点?提前致谢。

最佳答案

如果您希望 View 中的表开头有一个复选框,该复选框将传递给 Controller ​​,并且每行由 DoorID 唯一标识你可以尝试这样的事情。

在 View 中

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

在 Controller 中

public void ControllerName(int[] doorid)
{
foreach(var item in doorid)

//do something
}

关于javascript - 将 Model.ID 绑定(bind)到复选框列表并将 Model.X、Model.Y 等属性发布到 Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35311244/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com