Hi I have a problem with un-assigning a user from a Job. i.e I don't know how to delete a registration record related to the job that the user is assigned to. To make it more simple I have 3 tables with this relationship.
tblUser 1---------M tblRegistration M -----------1 tblJob
See this image of the form? Basically the end user clicks the big orange button "Unassign" and the record disappears from the table and appears in the table with the Unassigned Users heading.
https://imgur.com/jgVnTpQ
My problem is in the UnassignUserRegistration method where I am trying to delete my user registration which In my Job Controller. I will provide the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Pitcher.Data;
using Pitcher.Models;
using Pitcher.Models.TeamViewModels;
namespace Pitcher.Controllers
{
public class JobsController : Controller
{
private readonly TeamContext _context;
public JobsController(TeamContext context)
{
_context = context;
}
[HttpGet]
public async Task<IActionResult> AssignUserRegistration(int UserID, int JobID)
{
Registration registration = new Registration{UserID = UserID, JobID = JobID, RegistrationDate = DateTime.Now};
_context.Add(registration);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(UserAssignments), new{ID = JobID});
}
public IActionResult GetUnassignedUsers()
{
_context.Jobs.OrderByDescending(j => j.ID).FirstOrDefault();
var userlist = _context.Users.Where(u => !u.Registrations.Any());
return Json(userlist);
}
public IActionResult GetAssignedUsers()
{
_context.Jobs.OrderByDescending(j => j.ID).FirstOrDefault();
var userlist = _context.Users.Where(u => u.Registrations.Any());
return Json(userlist);
}
public async Task<IActionResult> UnassignUserRegistration(int UserID, int JobID)
{
if(JobID == null)
{
return NotFound();
}
var registration = await _context.Registrations.OrderByDescending(j => j.ID)
.AsNoTracking()
.Include(r => r.Job)
.Include(r => r.User)
.FirstOrDefaultAsync(m => m.ID == JobID);
if (registration == null)
{
ViewData["ErrorMessage"] =
"Delete failed. Try again, and if the problem persists " +
"see your system administrator.";
}
return RedirectToAction(nameof(UserAssignments), new{ID = JobID});
}
[HttpPost, ActionName("UnassignUserRegistration")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UnassignUserRegistration_Confirmed(int id)
{
var registration = await _context.Registrations.FindAsync(id);
if(registration == null)
{
return RedirectToAction(nameof(Index));
}
try
{
_context.Registrations.Remove(registration);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
catch(DbUpdateException )
{
return RedirectToAction(nameof(Delete), new {id = id, saveChangesError = true});
}
}
In the view file the method is being triggered in the second return of the the registeredUserTable. Here is all the code if you
need to see it. Here is the function I am rendering the table data to in the front end.
document.getElementById('registeredUsersTable').style.display = 'block';
var id=@Model.ID
$('#registeredUsersTable').DataTable({
"ajax": {
'type': 'get',
//PROBLEM:Cannot get the related UserID of the Job.
'data': { ID: id},
'dataType': "json",
"url": "@Url.Action("GetAssignedUsers")",
"dataSrc": function (result) {
return result;
}
},
"columns": [
{ "data": "userFirstName"},
{ "data": "userLastName"},
{ "data": "userContactEmail"},
{
"data": null,
"render": function (value) {
return '<a href="/Jobs/UnassignUserRegistration?UserID=' + value.id + '&JobID=' + id +' "button type="button" class="btn btn-primary btn-block">Unassign</a>';
}
}
]
});
What I have tried:
I looked all over online and used the original ASP.NET 5 Data Access Tutorial for MVC on Microsoft. The code samples are completely different to mine as I am attempting to pass different parameters.