Ejemplo n.º 1
0
        public ActionResult Update(EArticle model)
        {
            using (var db = new MyDbDataContext())
            {
                if (ModelState.IsValid)
                {
                    if (string.IsNullOrEmpty(model.Alias))
                    {
                        model.Alias = StringHelper.ConvertToAlias(model.Alias);
                    }
                    try
                    {
                        Article article = db.Articles.FirstOrDefault(b => b.ID == model.ID);
                        if (article != null)
                        {
                            article.MenuID          = model.MenuID;
                            article.Title           = model.Title;
                            article.Alias           = model.Alias;
                            article.Image           = model.Image;
                            article.Description     = model.Description;
                            article.Content         = model.Content;
                            article.MetaTitle       = string.IsNullOrEmpty(model.MetaTitle) ? model.Title : model.MetaTitle;
                            article.MetaDescription = string.IsNullOrEmpty(model.MetaDescription)
                                ? model.Title
                                : model.MetaDescription;
                            article.Status = model.Status;
                            article.Home   = model.Home;
                            article.Hot    = model.Hot;
                            article.New    = model.New;
                            article.Value  = model.Value;
                            article.About  = model.About;

                            db.SubmitChanges();
                            db.MenuTaps.DeleteAllOnSubmit(db.MenuTaps.Where(a => a.ArticleID == article.ID).ToList());
                            if (model.Theme != null)
                            {
                                for (int i = 0; i < model.Theme.Length; i++)
                                {
                                    var tabTheme = new MenuTap
                                    {
                                        ArticleID = article.ID,
                                        TapID     = int.Parse(model.Theme[i]),
                                    };
                                    db.MenuTaps.InsertOnSubmit(tabTheme);
                                    db.SubmitChanges();
                                }
                            }
                            TempData["Messages"] = "Successful";
                            return(RedirectToAction("Index"));
                        }
                    }
                    catch (Exception exception)
                    {
                        LoadData();
                        ViewBag.Messages = "Error: " + exception.Message;
                        return(View());
                    }
                }
                LoadData();
                return(View(model));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Create(EArticle model)
        {
            using (var db = new MyDbDataContext())
            {
                if (ModelState.IsValid)
                {
                    if (string.IsNullOrEmpty(model.Alias))
                    {
                        model.Alias = StringHelper.ConvertToAlias(model.Alias);
                    }
                    try
                    {
                        var article = new Article
                        {
                            MenuID          = model.MenuID,
                            Title           = model.Title,
                            Alias           = model.Alias,
                            Image           = model.Image,
                            Description     = model.Description,
                            Content         = model.Content,
                            Index           = 0,
                            MetaTitle       = string.IsNullOrEmpty(model.MetaTitle) ? model.Title : model.MetaTitle,
                            MetaDescription =
                                string.IsNullOrEmpty(model.MetaDescription) ? model.Title : model.Description,
                            Status     = model.Status,
                            Home       = model.Home,
                            Hot        = model.Hot,
                            New        = model.New,
                            Value      = model.Value,
                            About      = model.About,
                            CreateDate = DateTime.Now,
                        };

                        db.Articles.InsertOnSubmit(article);
                        db.SubmitChanges();
                        if (model.Theme != null)
                        {
                            for (int i = 0; i < model.Theme.Length; i++)
                            {
                                var tabTheme = new MenuTap
                                {
                                    ArticleID = article.ID,
                                    TapID     = int.Parse(model.Theme[i]),
                                };
                                db.MenuTaps.InsertOnSubmit(tabTheme);
                                db.SubmitChanges();
                            }
                        }

                        TempData["Messages"] = "Successful";
                        return(RedirectToAction("Index"));
                    }
                    catch (Exception exception)
                    {
                        LoadData();
                        ViewBag.Messages = "Error: " + exception.Message;
                        return(View());
                    }
                }
                LoadData();
                return(View());
            }
        }