예제 #1
0
        public PartialViewResult UniversalDelete(UniversalEditorPagedData ds, FormCollection collection)
        {
            ds.AddQueryParamsJoin = collection["AddQueryParamsJoin"];
            var    model  = GetModel(ds.CallerController, ds.CallerAction, null, ds.AddQueryParams);
            var    db     = new DB();
            var    table  = db.GetTableByName(model.Settings.TableName);
            object target = null;
            var    uid    = (int)model.EditedRow.GetPropertyValue(model.Settings.UIDColumnName);

            foreach (var item in table)
            {
                if ((int)item.GetPropertyValue(model.Settings.UIDColumnName) == uid)
                {
                    target = item;
                }
            }
            if (target == null)
            {
                ModelState.AddModelError("", "Объект не найден.");
                return(PartialView(model));
            }

            table.DeleteOnSubmit(target);
            db.SubmitChanges();
            model.RedirectURL = Url.Action(model.CallerAction, model.CallerController,
                                           new { Type = "List", Page = Request.QueryString["Page"].ToInt() });
            return(PartialView(model));
        }
예제 #2
0
        public PartialViewResult UniversalEdit(UniversalEditorPagedData ds, FormCollection collection)
        {
            ds.AddQueryParamsJoin = collection["AddQueryParamsJoin"];
            var model = GetModel(ds.CallerController, ds.CallerAction, null, ds.AddQueryParams);

            model.EditedRow.LoadPossibleProperties(new[] { "ID" }, collection);

            var imagePaths =
                collection.AllKeys.Where(x => x.EndsWith("_Path"))
                .Select(x => new { Field = x.Replace("_Path", ""), Data = ReadFile(collection[x]) }).Where(x => x.Data != null && x.Data.Length > 0);


            foreach (var image in imagePaths)
            {
                model.EditedRow.SetPropertyValue(image.Field, new Binary(image.Data));
            }


            var errList = model.ErrorList;

            if (errList.IsFilled())
            {
                var list = errList.Split <string>("<br/>");
                foreach (var err in list)
                {
                    ModelState.AddModelError("", err);
                }
                //ModelState.AddModelError("", errList);
                return(PartialView(model));
            }
            bool   inserting = (int)model.EditedRow.GetPropertyValue("ID") == 0;
            string msg       = "";

            if (model.SaveRow != null)
            {
                msg = model.SaveRow(model.EditedRow);
            }
            else
            {
                msg = model.Settings.UniversalTableSaver(model.EditedRow, model.Settings);
            }
            ModelState.AddModelError("", msg.IsNullOrEmpty() ? "Данные успешно сохранены" : msg);
            if (msg.IsNullOrEmpty())
            {
                if (inserting)
                {
                    var routes = new RouteValueDictionary
                    {
                        { "Type", "List" },
                        { "Page", Request.QueryString["Page"].ToInt() }
                    };
                    var filterRoutes = model.FilterParams;
                    foreach (var route in filterRoutes.Where(route => Request.QueryString[route].IsFilled()))
                    {
                        routes.Add(route, Request.QueryString[route]);
                    }
                    model.RedirectURL = Url.Action(model.CallerAction, model.CallerController, routes);
                }
            }
            return(PartialView(model));
        }
예제 #3
0
        public PartialViewResult UniversalDelete(UniversalEditorPagedData ds, FormCollection collection)
        {
            ds.AddQueryParamsJoin = collection["AddQueryParamsJoin"];
            var    model = GetModel(ds.CallerController, ds.CallerAction, null, ds.AddQueryParams);
            var    db    = new DB();
            ITable table = null;

            try
            {
                table = db.GetTableByName(model.Settings.TableName);
            }
            catch
            {
                try
                {
                    table = db.GetTableByName(model.Settings.TableName + "s");
                }
                catch
                {
                }
            }
            if (table == null)
            {
                ModelState.AddModelError("", "Таблица не найдена");
                return(PartialView(model));
            }
            object target = null;
            var    uid    = (int)model.EditedRow.GetPropertyValue(model.Settings.UIDColumnName);

            foreach (var item in table)
            {
                if ((int)item.GetPropertyValue(model.Settings.UIDColumnName) == uid)
                {
                    target = item;
                }
            }
            if (target == null)
            {
                ModelState.AddModelError("", "Объект не найден.");
                return(PartialView(model));
            }
            if (model.BeforeDelFunc != null)
            {
                model.BeforeDelFunc(target, db);
            }
            try
            {
                if (model.DelFunc != null)
                {
                    model.DelFunc(target, db);
                }
                else
                {
                    table.DeleteOnSubmit(target);
                    db.SubmitChanges();
                }
            }
            catch (Exception ee)
            {
                ModelState.AddModelError("", ee.Message);
                return(PartialView(model));
            }
            RouteValueDictionary dict = new RouteValueDictionary();

            dict.Add("Type", "List");
            dict.Add("Page", Request.QueryString["Page"].ToInt());
            if (ds.AddQueryParams != null)
            {
                foreach (var param in ds.AddQueryParams)
                {
                    if (!dict.ContainsKey(param) && Request.QueryString[param].IsFilled())
                    {
                        dict.Add(param, Request.QueryString[param]);
                    }
                }
            }
            model.RedirectURL = Url.Action(model.CallerAction, model.CallerController, dict);
            return(PartialView(model));
        }