I have parent view "index.cshtml" and partial view"_AddEmployeePartialView.cshtml". I'm currently trying to figure out how to disable a button "Add Employees" once the button "Save" in the partial view is clicked and partial view is closed of course. I'm not good at javascript so I appreciate any help I may receive. this is the code i used:
index.chtml:
@model IEnumerable<Department>
@{
ViewData["Title"] = "Department";
}
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.DepId)
</th>
<th>
@Html.DisplayNameFor(model => model.DepName)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DepId)
</td>
<td>
@Html.DisplayFor(modelItem => item.DepName)
</td>
<td>
<div class="btn-group-sm">
<button class="btn btn-primary" data-toggle="modal" data-target="@("#AddEmp-"+item.DepId)" data-url="@Url.Action($"AddEmployee/{item.DepId}")" id="btnAdd" >Add Employees</button>
@await Html.PartialAsync("_AddEmployeePartialView", item)
<script>
$('#AddEmp-' + @Html.Raw(item.DepId)).on('click', '#btnSave', function () {
document.getElementById("btnAdd").disabled = true;
</script>
</div>
</td>
</tr>
}
</tbody>
</table>
_AddEmployeePartialView.cshtml:
@model management.Models.Employee
@{
ViewData["Title"] = "_AddEmployeePartialView";
}
<div class="modal fade" role="dialog" tabindex="-1" id="@("AddEmp-"+Model.DepId)" aria-labelledby="addEmpLabel" aria-hidden="true" >
<div class="modal-dialog " role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Employees</h3>
</div>
<div class="modal-body">
<form asp-action="AddEmployee" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input asp-for="DepId" type="hidden" />
<table class="table table-bordered" >
<thead>
<tr>
<th>Employee name</th>
<th>Profession</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Employees.Count; i++)
{
<tr>
<td>
@Html.EditorFor(x => x.Employees[i].EmpName, new { htmlAttributes = new { @class = "form-control" } })
</td>
<td>
@Html.EditorFor(x => x.Employees[i].Profession, new { htmlAttributes = new { @class = "form-control" } })
</td>
<td>
@Html.EditorFor(x => x.Employees[i].Phone, new { htmlAttributes = new { @class = "form-control" } })
</td>
</tr>
}
</tbody>
</table>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" onclick="javascript:window.location.reload()">Cancel</button>
<button type="submit" class="btn btn-primary" id="btnSave">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
This code is not working, the button in the index view isn't disabled. how to solve this?