public ActionResult MapRoleModuleAction(MapRoleModuleActionsViewModel model) { if (!ModelState.IsValid) { ViewBag.MapList = GetMapLists(); ViewBag.ErrorMessage = "Please check inputs"; return(View(model)); } try { using (KDMDB db = new KDMDB()) { var rw = db.tbl_RoleActionMappings.Where(x => x.Role == model.Role && x.Area == model.Module && x.Controller == model.Controller && x.Action == model.Action).Select(x => x).FirstOrDefault(); if (rw != null) { ViewBag.MapList = GetMapLists(); ViewBag.ErrorMessage = "Entry already exists"; return(View(model)); } tbl_RoleActionMappings tbl = new tbl_RoleActionMappings(); tbl.Role = model.Role; tbl.Area = model.Module; tbl.ControllerDisplayText = db.tbl_modules.Where(x => x.Value == model.Module).Select(x => x.Text).FirstOrDefault(); tbl.Controller = model.Controller; tbl.ControllerDisplayText = db.tbl_controllers.Where(x => x.Value == model.Controller).Select(x => x.Text).FirstOrDefault(); tbl.Action = model.Action; tbl.ActionDisplayText = db.tbl_actions.Where(x => x.Value == model.Action).Select(x => x.Text).FirstOrDefault(); tbl.IsActive = false; db.tbl_RoleActionMappings.Add(tbl); db.SaveChanges(); ViewBag.SuccessMessage = "Successfully saved"; } } catch { ViewBag.ErrorMessage = "Can't save"; } ViewBag.MapList = GetMapLists(); return(View(model)); }
public void CreateRoute(string role, UserRoute newRoute) { using (KDMDB db = new KDMDB()) { using (var tr = db.Database.BeginTransaction()) { try { tbl_RoleActionMappings tbl = new tbl_RoleActionMappings(); tbl.Area = newRoute.Area; tbl.Controller = newRoute.Controller; tbl.Action = newRoute.Action; tbl.IsActive = newRoute.IsActive; db.tbl_RoleActionMappings.Add(tbl); db.SaveChanges(); Int64 newRouteId = tbl.ID; tbl_RoleActions roleAction = db.tbl_RoleActions .Where(p => p.Role == role) .Select(t => t).FirstOrDefault(); List <Int64> actionIds = JsonConvert.DeserializeObject <List <Int64> >(roleAction.Actions); actionIds.Add(newRouteId); } catch { tr.Rollback(); } finally { tr.Commit(); } } } }