Exemplo n.º 1
0
        public ActionResult Create(Articles art, HttpPostedFileBase dataFile)
        {
            try
            {
                IArticlesService artSrv = IoC.Resolve <IArticlesService>();
                art.CreatedBy = HttpContext.User.Identity.Name;
                art.URLName   = FX.Utils.Common.TextHelper.ToUrlFriendly(art.NameVNI);
                if (!art.IsEvent)
                {
                    art.StartDate = DateTime.Now;
                    art.EndDate   = DateTime.Now;
                }
                if (dataFile != null && dataFile.ContentLength > 0)
                {
                    art.TypeData = dataFile.FileName.Substring(dataFile.FileName.LastIndexOf("."));
                    byte[] bf = new byte[dataFile.ContentLength];
                    dataFile.InputStream.Read(bf, 0, dataFile.ContentLength);
                    art.AttachData = bf;
                }
                artSrv.CreateNew(art);
                // tu động approved
                art.Approved  = true;
                art.ApproveBy = HttpContext.User.Identity.Name;
                art.Active    = true;

                artSrv.CommitChanges();
                Messages.AddFlashMessage("Thêm mới thành công.");

                logSrv.CreateNew(FXContext.Current.CurrentUser.userid, "Article - Create:" + art.Id, "Create Article complete", LogType.Success, HttpContext.Request.UserHostAddress, HttpContext.Request.Browser.Browser);
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                logSrv.CreateNew(FXContext.Current.CurrentUser.userid, "Article - Create:" + art.Id, "Create Error" + ex, LogType.Error, HttpContext.Request.UserHostAddress, HttpContext.Request.Browser.Browser);
                Messages.AddErrorMessage("Có lỗi xảy ra, vui lòng thực hiện lại.");
                ICategoryService artCateSrv = IoC.Resolve <ICategoryService>();
                ArticleModels    model      = new ArticleModels();
                model.Article = art;

                model.Categories = artCateSrv.Query.Where(p => p.Active).OrderBy(p => p.NameVNI).ThenBy(p => p.NameENG).ToList();
                return(View(model));
            }
        }