As part of my application, I need to create a function that allows for the changing of deputy priorities for each consultant. Here is what I have implemented so far:
View:
@model ML.Domain.DAL.DB_CONSULTANTS
....
<table>
<tr>
<th>ID Deputy</th>
<th>Priority</th>
<th></th>
<th></th>
<th></th>
</tr>
@foreach (var item in Model.DB_CONSULTANT_DEPUTY.OrderBy(x => x.PRIORITY))
{
<tr>
<td>@item.DEPUTY_ID</td>
<td>@item.PRIORITY</td>
<td><button class="btn btn-default up" data-consultant-id="@item.CONSULTANT_ID" data-deputy-id="@item.DEPUTY_ID" data-priority="@item.PRIORITY">UP</button></td>
<td><button class="btn btn-default down" data-consultant-id="@item.CONSULTANT_ID" data-deputy-id="@item.DEPUTY_ID" data-priority="@item.PRIORITY">DOWN</button></td>
<td><button class="btn btn-default delete" data-consultant-id="@item.CONSULTANT_ID" data-deputy-id="@item.DEPUTY_ID" data-priority="@item.PRIORITY">Remove</button></td>
</tr>
}
</table>
Script (currently only for removing deputy):
<script>
var url = '@Url.Action("RemoveDeputy", "Consultant")';
$('.delete').click(function () {
var container = $(this).closest('tr');
var data = { consultant_Id: $(this).data('consultant-id'), deputy_Id: $(this).data('deputy-id'), priority: $(this).data('priority') };
$.post(url, data, function (response) {
if (response)
{
// fadeout, then remove
container.fadeOut(800, function () {
$(this).remove();
});
} else
{
alert("Error!");
}
}).fail(function () {
alert("Error!");
});
});
</script>
Backend:
[HttpPost]
public JsonResult RemoveDeputy(DB_CONSULTANT_DEPUTY model)
{
consultantRepository.RemoveDeputy(model);
return Json(true);
}
I also need to add a similar function to the script, where if a user clicks on the UP
or DOWN
button, the same data as for DELETE
will be sent to the backend and the priority will be changed accordingly. For example:
[HttpPost]
public JsonResult ChangePriorityToUp(DB_CONSULTANT_DEPUTY model)
{
var deputyForChangePriority = db.DB_CONSULTANT_DEPUTY.Find(model.DEPUTY_ID);
deputyForChangePriority.PRIORITY -= 1;
if (db.DB_CONSULTANT_DEPUTY.Any(x => x.PRIORITY == deputyForChangePriority.PRIORITY + 1))
{
var deputyToRefresh = db.DB_CONSULTANT_DEPUTY.First(x => x.PRIORITY == deputyForChangePriority.PRIORITY + 1);
deputyToRefresh.PRIORITY -= 1;
}
db.SaveChanges();
return Json(true);
}
Lastly, I need to figure out how to refresh the table view after these changes are made using JavaScript functions. Any help with this part would be greatly appreciated.