public JsonResult Delete(int id = 0)
 {
     Admin.Models.BaseController model = db.BaseControllers.Find(id);
     if (model != null)
     {
         db.BaseControllers.Remove(model);
         db.SaveChanges();
     }
     return(Json("ok", JsonRequestBehavior.AllowGet));
 }
 public ActionResult Form(int id = 0)
 {
     if (id == 0)
     {
         return(PartialView(new Admin.Models.BaseController()));
     }
     else
     {
         Admin.Models.BaseController model = db.BaseControllers.Find(id);
         return(PartialView(model));
     }
 }
 public JsonResult Save(Admin.Models.BaseController model)
 {
     if (model.id != 0)
     {
         if (ModelState.IsValid)
         {
             db.Entry(model).State = EntityState.Modified;
             db.SaveChanges();
         }
     }
     else
     {
         db.BaseControllers.Add(model);
         db.SaveChanges();
     }
     return(Json(model.id));
 }
Exemple #4
0
        /// <summary>
        /// Metodo utilizado para almacenar los nombres de los controladores
        /// y sus acciones asociadas en la base de datos.
        /// </summary>
        public static void InsertControllerAndActions()
        {
            ControllerActionProvider cp = new ControllerActionProvider();

            using (Context db = new Context())
            {
                List <BaseController> Controllers  = db.BaseControllers.ToList();
                List <string>         existingName = new List <string>();
                foreach (BaseController controller in Controllers)
                {
                    if (existingName.Contains(controller.name))
                    {
                        foreach (BaseAction item in controller.BaseActions.ToList())
                        {
                            db.BaseActions.Remove(item);
                        }
                        db.BaseControllers.Remove(controller);
                        db.SaveChanges();
                    }
                    else
                    {
                        existingName.Add(controller.name);
                    }
                }

                foreach (string controllerName in cp.GetControllerNames())
                {
                    if (controllerName.Contains("User"))
                    {
                    }
                    bool insertNew = false;
                    Admin.Models.BaseController c = db.BaseControllers.FirstOrDefault(t => t.fullName == controllerName);
                    if (c == null)
                    {
                        insertNew  = true;
                        c          = new Admin.Models.BaseController();
                        c.fullName = controllerName;
                        c.infoName = controllerName.Replace("Controller", "");
                        c.name     = c.infoName;
                    }


                    List <Admin.Models.BaseAction> actions = new List <Admin.Models.BaseAction>();
                    string[] lstActions = cp.GetActionNames(controllerName).Distinct().ToArray();

                    foreach (string action in lstActions)
                    {
                        Admin.Models.BaseAction a = new Admin.Models.BaseAction();
                        a.name        = action;
                        a.displayName = Helper.Capitalize(action);
                        if (!c.BaseActions.Select(d => d.name).Contains(action))
                        {
                            c.BaseActions.Add(a);
                        }
                    }

                    List <BaseAction> actionsToDelete = c.BaseActions.ToList();
                    foreach (BaseAction item in actionsToDelete)
                    {
                        if (!lstActions.Contains(item.name))
                        {
                            Helper.executeNonQUery("UPDATE BaseMenu Set actionID=null where actionID=" + item.id.ToString(), db);
                            Helper.executeNonQUery("UPDATE BaseWidget Set actionID=null where actionID=" + item.id.ToString(), db);
                            db.BaseActions.Remove(item);
                            try
                            {
                                db.SaveChanges();
                            }
                            catch
                            {
                            }
                        }
                    }

                    List <string> existingActionName = new List <string>();
                    foreach (BaseAction action in actionsToDelete)
                    {
                        if (existingActionName.Contains(action.name))
                        {
                            //db.BaseActions.Remove(action);
                            //db.SaveChanges();
                        }
                        else
                        {
                            existingActionName.Add(action.name);
                        }
                    }

                    if (insertNew)
                    {
                        db.BaseControllers.Add(c);
                        db.SaveChanges();
                    }
                    else
                    {
                        db.Entry(c).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                }
            }
        }