Ejemplo n.º 1
0
        public ActionResult SaveNews(SA_News UserNews)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i];

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);

                    var path = Path.Combine(Server.MapPath("~/ProductImages"), fileName);
                    file.SaveAs(path);
                    UserNews.NewsImg = fileName;
                }
            }
            UserNews.status      = 1;
            UserNews.CreatedBy   = Session["User"].ToString();
            UserNews.CreatedTime = UserNews.CreatedTime != null? UserNews.CreatedTime: DateTime.Now;
            NewsDataStore Obj = new NewsDataStore();

            if (UserNews.id == 0)
            {
                Obj.AddNews(UserNews);

                string np = Request.Form["allProduct"].ToString();

                foreach (string s in np.Split(','))
                {
                    if (s != "")
                    {
                        Obj.AddNewsProduct(new SA_NewsAndProductRelation {
                            SA_NewsId    = UserNews.id,
                            SA_ProductId = Convert.ToInt16(s)
                        });
                    }
                }
            }
            else
            {
                Obj.EditNews(UserNews);

                string np = Request.Form["allProduct"].ToString();
                Obj.DeleteNewsProduct(UserNews.id);
                foreach (string s in np.Split(','))
                {
                    if (s != "")
                    {
                        Obj.AddNewsProduct(new SA_NewsAndProductRelation
                        {
                            SA_NewsId    = UserNews.id,
                            SA_ProductId = Convert.ToInt16(s)
                        });
                    }
                }
            }
            return(RedirectToAction("News"));
        }