public ActionResult AddNoticeToDatabase(NewNoticeViewModel model) { try { var newNoticeToAdd = new NoticeEntity(); newNoticeToAdd.Amount = decimal.Parse(model.Amount); newNoticeToAdd.CategoryId = int.Parse(model.SelectedCategoryId); newNoticeToAdd.CreateDate = DateTime.Now; newNoticeToAdd.Description = model.Description; newNoticeToAdd.ExprireDate = DateTime.Now.AddDays(int.Parse(model.ExpireTo)); newNoticeToAdd.NoticePlaceId = int.Parse(model.NoticePlaceId); newNoticeToAdd.Title = model.Title; newNoticeToAdd.UserProfileId = WebMatrix.WebData.WebSecurity.CurrentUserId; var noticeRepo = new NoticeRepository(); noticeRepo.AddNewNotice(newNoticeToAdd); ViewBag.Message = "Dodano ogłoszenie."; } catch (Exception ex) { ViewBag.Message = "Ups coś poszło nie tak."; ViewBag.ExtendMessage = ex.Message; } return View(); }
public void AddNewNotice(NoticeEntity notice) { using (var context = new UsersContext()) { context.Notices.Add(notice); context.SaveChanges(); } }
public void UpdateNotice(NoticeEntity noticeToUpdate) { using (var context = new UsersContext()) { var notice = context.Notices.FirstOrDefault(x => x.Id == noticeToUpdate.Id); notice.Amount = noticeToUpdate.Amount; notice.CategoryId = noticeToUpdate.CategoryId; notice.Description = noticeToUpdate.Description; notice.NoticePlaceId = noticeToUpdate.NoticePlaceId; notice.Title = noticeToUpdate.Title; context.SaveChanges(); } }