0

I have a view code in my view, how do I get the data from the table in my controller from the view?
I have rows in a table, I need to pass the employee IDs of the selected rows to the controller. For the selected row, I have my css class "Selected".

View code:

@model IEnumerable<WebProductionAccounting.Domain.Entities.Employee>
  
@{
    ViewBag.Tittle = "List of employees";
    Layout = "_Layout";
}
<div сlass ="btn-group">
 <button type="button" class="button_hola" id ="btn-eml-add">
 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-person-add" viewBox="0 0 16 16">
  <path d="M12.5 16a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7Zm.5-5v1h1a.5.5 0 0 1 0 1h-1v1a.5.5 0 0 1-1 0v-1h-1a.5.5 0 0 1 0-1h1v-1a.5.5 0 0 1 1 0Zm-2-6a3 3 0 1 1-6 0 3 3 0 0 1 6 0ZM8 7a2 2 0 1 0 0-4 2 2 0 0 0 0 4Z"></path>
  <path d="M8.256 14a4.474 4.474 0 0 1-.229-1.004H3c.001-.246.154-.986.832-1.664C4.484 10.68 5.711 10 8 10c.26 0 .507.009.74.025.226-.341.496-.65.804-.918C9.077 9.038 8.564 9 8 9c-5 0-6 3-6 4s1 1 1 1h5.256Z"></path>
 </svg>
 New employee
</button>
</div>
<br />
<!--Table Emloyees-->
<table class="table">
<thead>
    <tr id ="tr-hover">
        <th scope="col" class ="visually-hidden">Id</th>
        <th scope="col">№</th>
        <th scope="col">Lastname</th>
        <th scope="col">Firstname</th>
        <th scope="col">Middlename</th>
        <th scope="col">PersonnelNumber</th>
        <th scope="col">Position</th>
        <th>DateOfEmployment</th>
    </tr>
</thead>
<tbody class="table-group-divider">
        @{var employees = Model.ToList();}
        @for (int i = 0; i < employees.Count; i++)
        {
            var number_row = i + 1;
            <tr>
                <td class ="visually-hidden">@employees[i].Id</td>
                <td>@number_row</td>
                <td>@employees[i].Lastname</td>
                <td>@employees[i].Firstname</td>
                <td>@employees[i].Middlename</td>
                <td>@employees[i].PersonnelNumber</td>
                <td>@employees[i].Position</td>
                <td>@employees[i].DateOfEmployment</td>  
            </tr>             
        }
</tbody>
</table>

Controller code:

        [HttpGet]
        public async Task<IActionResult> GetEmployee(int id)
        {
         var response = await _employeeService.GetEmployee(id);
            if(response.StatusCode == Domain.Enum.StatusCode.OK)
            {
                return View(response.Data);
            }
            return RedirectToAction("Error");
        }

JS:

var trs = document.querySelectorAll("tbody tr");

for (var i = 0; i < trs.length; i++) {
    MakeRowHover(trs[i]);
    SelectRow(trs[i]);
}

function SelectRow(row) {
    //row click
    row.addEventListener('click', function () {
        if (!this.classList.contains('tr-selected')) {
            this.classList.add('tr-selected');
        } else {
            this.classList.remove('tr-selected');
        }
    });
}

I read the documentation, but to be honest, I did not find a specific example for my case.

  • Hi @Stanislav Mikhailov you need to make a ajax call to your controller. This might [help](https://stackoverflow.com/questions/16186083/making-a-simple-ajax-call-to-controller-in-asp-net-mvc) – Manik Feb 16 '23 at 14:22

0 Answers0