Пример #1
0
        public ActionResult Item(int bannerId, int id = 0)
        {
            var model = new WB_AnyBannerItemModel();

            if (id == 0)
            {
                var newItem = new WB_AnyBannerItemModel
                {
                    BannerID  = bannerId,
                    IsActived = true,
                };

                model = newItem;
            }
            else
            {
                var item = _AnyBannerItemRepo.GetById(id);
                model.Id        = item.Id;
                model.BannerID  = item.BannerID;
                model.Title     = item.Title;
                model.Url       = item.Url;
                model.ImageUrl  = item.ImageUrl;
                model.IsActived = item.IsActived;
            }
            return(PartialView("~/Plugins/Worldbuy.AnyBanner/Views/_CreateOrUpdate.Item.cshtml", model));
        }
Пример #2
0
        public ActionResult SaveItem(WB_AnyBannerItemModel model)
        {
            bool   status   = false;
            string message  = "Error";
            string fileName = "";

            if (ModelState.IsValid)
            {
                try
                {
                    if (model.Id == 0)
                    {
                        if (Request.Files != null && Request.Files.Count > 0)
                        {
                            for (int i = 0; i < Request.Files.Count; i++)
                            {
                                HttpPostedFileBase file = Request.Files[i];
                                if (file != null && file.ContentLength > 0)
                                {
                                    var folder     = "~/Content/Images/Worldbuy.AnyBanner/Images";
                                    var folderPath = Server.MapPath(folder);
                                    if (!System.IO.Directory.Exists(folderPath))
                                    {
                                        System.IO.Directory.CreateDirectory(folderPath);
                                    }

                                    var entity = new WB_AnyBannerItem
                                    {
                                        BannerID  = model.BannerID,
                                        Id        = 0,
                                        IsActived = model.IsActived,
                                        Title     = model.Title,
                                        Url       = model.Url,
                                        Alt       = model.Alt,
                                    };
                                    _AnyBannerItemRepo.Insert(entity);
                                    if (entity.Id > 0)
                                    {
                                        fileName        = folder + "/" + entity.Id.ToString("000000") + System.IO.Path.GetExtension(file.FileName);
                                        entity.ImageUrl = Url.Content(fileName);
                                        _AnyBannerItemRepo.Update(entity);
                                        string filePath = Server.MapPath(fileName);
                                        file.SaveAs(filePath);
                                    }
                                }
                            }
                        }
                        status  = true;
                        message = "Success";
                    }
                    else
                    {
                        var entity = _AnyBannerItemRepo.GetById(model.Id);
                        if (entity != null)
                        {
                            entity.Title     = model.Title;
                            entity.Url       = model.Url;
                            entity.IsActived = model.IsActived;
                            entity.Alt       = model.Alt;
                            entity.Order     = model.Order;
                            _AnyBannerItemRepo.Update(entity);
                        }
                        model = _AnyBannerItemService.GetModelById(model.Id);
                        return(Json(model));
                    }
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                    LogException(ex);
                }
            }


            return(Json(new { Status = status, Message = message }));
        }