-1

In an MVC application I joined multiple tables and returned it from Controller to View as shown below:

| 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());
}

I want to bind DoorName values (Door 1, Door 2, Door 3, Door 4) to a checkbox list and let the user to select them. After that, I want to pass corresponding EmployeeID, ControlID, DoorAddress, DoorID values of the the selected door to the Controller. For example if the user selects Door 1 and Door 3, then I will pass these values below to the Controller:

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

By using razor syntax or javascript in the view, how can I do this? Thanks in advance.

  • Use a view model including a `bool IsSelected` property so you can bind it to a checkbox. Refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Feb 10 '16 at 09:32
  • @StephenMuecke Thanks, but the given answers does not have a checkbox list. Could you please give an example regarding to my question (I returned the given list above by using sql query) having checkbox list? –  Feb 10 '16 at 09:54
  • Of course it does! Study it - each item in the collection has a checkbox associated with it. When the form is submitted you can query the returned collection and get the selected items using `.Where(m => m.IsSelected)` –  Feb 10 '16 at 09:56
  • @StephenMuecke Thanks a lot. I post the selected IDs, but cannot bind the checkboxes according to the values they have (for example it would be enough for me to bind only the checkboxes that have ID value). Is it possible? –  Feb 10 '16 at 19:27
  • Sorry, but you have not shown the new code you are now trying so impossible to understand. Either append the new code to this question or ask a new question explaining what problems your having. –  Feb 10 '16 at 21:12

1 Answers1

0

If you want a checkbox at the beginning of the Table in the view which is to be passed to the controller and each row is uniquely identified by the DoorID you could try something like this.

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

In controller

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

          //do something
}
Vini
  • 1,978
  • 8
  • 40
  • 82
  • Many thanks for your answer... I managed to post the selected IDs, but I cannot bind the checkbox although I used @Stephen's isSelected property in the model. Any idea? –  Feb 10 '16 at 19:24
  • On the other hand I tried, but cannot vote up as I have not enough reputation :( –  Feb 10 '16 at 19:28