Example #1
0
        public void Save(Infos data)
        {
            DateTime today = DateTime.Now;

            //新增
            if (data.Id == 0)
            {
                data.CreateDate = today;
                data.ModifyDate = today;
                context.Infos.Add(data);
            }
            else
            {
                //編輯
                Infos dbEntry =
                    context.Infos.FirstOrDefault(p => p.Id == data.Id);
                if (dbEntry != null)
                {
                    dbEntry.Title       = data.Title;
                    dbEntry.PublishDate = data.PublishDate;
                    dbEntry.OffDateTime = data.OffDateTime;
                    dbEntry.HtmlContent = data.HtmlContent;
                    dbEntry.ModifyDate  = today;
                }
            }
            context.SaveChanges();
        }
Example #2
0
        public ActionResult Edit(InfoEditViewModel vm, string[] selectedTags)
        {
            if (ModelState.IsValid)
            {
                DateTime today = DateTime.Now;
                Infos    dbEntry;
                if (vm.ID == 0)
                {
                    dbEntry = new Infos()
                    {
                        Title       = vm.Title,
                        HtmlUrl     = vm.HtmlUrl,
                        Dtype       = 1,
                        CreateDate  = today,
                        PublishDate = vm.PublishDate,
                        OffDateTime = vm.OffDateTime,
                        HtmlContent = vm.HtmlContent
                    };
                    //新增
                    context.Infos.Add(dbEntry);
                }
                else
                {
                    //編輯
                    dbEntry =
                        context.Infos
                        .Include(s => s.InfoTag)
                        .ThenInclude(e => e.Tag)
                        .FirstOrDefault(p => p.Id == vm.ID);

                    if (dbEntry != null)
                    {
                        dbEntry.Title       = vm.Title;
                        dbEntry.HtmlUrl     = vm.HtmlUrl;
                        dbEntry.Dtype       = 1;
                        dbEntry.PublishDate = vm.PublishDate;
                        dbEntry.OffDateTime = vm.OffDateTime;
                        dbEntry.HtmlContent = vm.HtmlContent;
                        dbEntry.ModifyDate  = today;
                        //UpdateInstructorTags(selectedTags, dbEntry);
                    }
                }
                UpdateInstructorTags(selectedTags, dbEntry);
                context.SaveChanges();
                TempData["message"] = $"已經儲存!";
                return(RedirectToAction("List"));
            }
            else
            {
                var infoTag =
                    new HashSet <int>(selectedTags.Select(c => Convert.ToInt32(c)));
                var viewModel = new List <SelectListViewModel>();
                foreach (var _tag in context.Tag)
                {
                    viewModel.Add(new SelectListViewModel
                    {
                        Id       = _tag.Id,
                        Name     = _tag.TagName,
                        Assigned = infoTag.Contains(_tag.Id)
                    });
                }
                ViewData["Tags"] = viewModel;
                return(View(vm));
            }
            //return View(vm);
        }