public ActionResult CountryRoleUserAdd(FormCollection collection) { if (!Permission.CanUpdatePermissions) { return(new HttpUnauthorizedResult("User does not have permission to Add Country Role Users")); } int.TryParse(collection["UserID"], out int userID); int.TryParse(collection["CountryRoleID"], out int countryRoleID); CountryRoleUser countryRoleUser = new CountryRoleUser(); countryRoleUser.UserID = userID; countryRoleUser.CountryRoleID = countryRoleID; Context context = new Context(); context.CountryRoleUsers.Add(countryRoleUser); try { context.SaveChanges(); } catch (DbEntityValidationException ex) { return(ModalSaveFailedResult(ex)); } return(Json(new { success = true })); }
public ActionResult CountryRoleUserDelete(long?id) { if (!Permission.CanUpdatePermissions) { return(new HttpUnauthorizedResult("User does not have access to update permissions")); } Context context = new Context(); CountryRoleUser countryRoleUser = context.CountryRoleUsers.FirstOrDefault(cru => cru.CountryRoleUserID == id); if (countryRoleUser == null) { return(HttpNotFound("Could not find Role User")); } long?countryRoleID = countryRoleUser.CountryRoleID; context.CountryRoleUsers.Remove(countryRoleUser); context.SaveChanges(); return(RedirectToAction("CountryDetails", new { countryid = CountryID, id = countryRoleID })); }
public ActionResult CountryRoleAdd(CountryRole countryRole) { if (!Permission.CanUpdatePermissions) { return(new HttpUnauthorizedResult("User does not have permission to add Country Roles")); } countryRole.CountryRoleUsers = new List <CountryRoleUser>(); foreach (string key in Request.Form.AllKeys.Where(key => key.StartsWith("CountryRoleUser-"))) { if (!int.TryParse(Request.Form[key], out int userID)) { continue; } CountryRoleUser countryRoleUser = new CountryRoleUser(); countryRoleUser.UserID = userID; countryRole.CountryRoleUsers.Add(countryRoleUser); } Context context = new Context(); context.CountryRoles.Add(countryRole); try { context.SaveChanges(); } catch (DbEntityValidationException ex) { return(ModalSaveFailedResult(ex)); } return(Json(new { success = true })); }
public ActionResult CountryRoleAdd(CountryRole countryRole) { if (!permission.CanAddCountries) { return(new HttpUnauthorizedResult("User does not have permission to Add Country Roles")); } Context context = new Context(); using (DbContextTransaction transaction = context.Database.BeginTransaction()) { try { context.CountryRoles.Add(countryRole); try { context.SaveChanges(); } catch (DbEntityValidationException ex) { return(ModalSaveFailedResult(ex)); } Dictionary <string, string> errors = new Dictionary <string, string>(); Dictionary <long?, string> keyByUserID = new Dictionary <long?, string>(); foreach (string key in Request.Form.AllKeys.Where(aKey => aKey.StartsWith("CountryRoleUser-"))) { if (!long.TryParse(Request.Form[key], out long userID)) { errors.Add(key, "User ID is not valid"); continue; } CountryRoleUser countryRoleUser = new CountryRoleUser(); countryRoleUser.CountryRoleID = countryRole.CountryRoleID.Value; countryRoleUser.UserID = userID; context.CountryRoleUsers.Add(countryRoleUser); if (keyByUserID.ContainsKey(userID)) { errors.Add(key, "User must be unique"); } keyByUserID[userID] = key; } if (errors.Any()) { return(Json(new { success = false, errors })); } try { context.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult entityValidationResult in ex.EntityValidationErrors) { long?userID = entityValidationResult.Entry.Property("UserID").CurrentValue as long?; if (keyByUserID.ContainsKey(userID)) { errors[keyByUserID[userID]] = entityValidationResult.ValidationErrors.FirstOrDefault().ErrorMessage; } } } if (errors.Any()) { return(Json(new { success = false, errors })); } transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } return(Json(new { success = true })); }