将Model.ID绑定到Checkbox列表并将Model.X,Model.Y等属性发布到Controller

时间:2016-02-10 09:29:44

标签: javascript c# asp.net-mvc checkbox checkboxlist

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并让用户选择它们。之后,我想将所选门的相应EmployeeIDControlIDDoorAddressDoorID值传递给Controller。例如,如果用户选择了Door 1和Door 3,那么我将以下值传递给Controller

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

在视图中使用razor语法或javascript,我该怎么做?提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果您希望在视图中表格的开头有一个复选框,该复选框将传递给控制器​​,而每一行都由function translate(message, p) { var alphabet = 'abcdefghijklmnopqrstuvwxyz', i, pos, result = ''; for (i = 0; i < message.length; i++) { pos = alphabet.indexOf(message[i]); result += alphabet[(pos + p) % alphabet.length]; } return result; } document.write(translate("bbcd", 2));唯一标识,您可以尝试这样的事情。

在视图

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>