コード例 #1
0
        public ActionResult Create(ManageBookmarkDataViewModel bookmarkModel)
        {
            if (string.IsNullOrEmpty(bookmarkModel.Title))
            {
                bookmarkModel.Title = bookmarkModel.Url;
            }
            var user = GetUser();
            var existing = db.Bookmarks.Where(x => x.Url == bookmarkModel.Url && x.User.UserId == user.UserId).FirstOrDefault();
            if (existing != null)
            {
                if (ModelState.IsValid && existing.User.UserId == user.UserId)
                {
                    existing.IsDeleted = false;
                    existing.LastEdit = DateTime.Now;
                    existing.Title = bookmarkModel.Title;
                    existing.Url = bookmarkModel.Url;
                    existing.Description = bookmarkModel.Description;
                    existing.Category = LoadOrCreateCategory(bookmarkModel.Category);
                }
            }
            else
            {
                if (ModelState.IsValid)
                {
                    var newBookmark = new Bookmark();
                    newBookmark.IsDeleted = false;
                    newBookmark.LastEdit = DateTime.Now;
                    newBookmark.Title = bookmarkModel.Title;
                    newBookmark.Url = bookmarkModel.Url;
                    newBookmark.Description = bookmarkModel.Description;
                    newBookmark.User = user;
                    newBookmark.Category = LoadOrCreateCategory(bookmarkModel.Category);

                    db.Bookmarks.Add(newBookmark);
                }
            }

            db.SaveChanges();
            return RedirectToAction("Index");
        }
コード例 #2
0
        public ActionResult Edit(int id = 0)
        {
            Bookmark bookmark = db.Bookmarks.Find(id);
            if (bookmark == null)
            {
                return HttpNotFound();
            }

            ManageBookmarkDataViewModel model = new ManageBookmarkDataViewModel()
            {
                Id = bookmark.Id,
                Description = bookmark.Description,
                Title = bookmark.Title,
                Url = bookmark.Url,
                Category = bookmark.Category.Name
            };

            var user = GetUser();
            var userCategories = db.Categories
                .Where(x => x.User.UserId == user.UserId && !x.IsDeleted)
                .Select(x => new SelectListItem
                {
                    Text = x.Name,
                    Value =x.Name,
                    Selected = x.Id==bookmark.Category.Id,
                })
                .ToList();

            ViewData["Category"] = userCategories;

            return View(model);
        }