示例#1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Content,CategoryId,Image,LastModificationTime")] PostModel postModel)
        {
            if (id != postModel.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var post = await _context.Posts.Where(x => x.Id == id)
                               .FirstOrDefaultAsync();

                    post.Title                = postModel.Title;
                    post.Content              = postModel.Content;
                    post.Image                = postModel.Image;
                    post.CategoryId           = postModel.CategoryId;
                    post.LastModificationTime = DateTime.UtcNow;

                    _context.Update(post);

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PostModelExists(postModel.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Set <CategoryModel>(), "Id", "Id", postModel.CategoryId);
            return(View(postModel));
        }
示例#2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Description,Image, CategoryType, LastModificationTime")] CategoryModel categoryModel)
        {
            if (id != categoryModel.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var category = await _context.Categories.Where(x => x.Id == id)
                                   .FirstOrDefaultAsync();

                    category.Name                 = categoryModel.Name;
                    category.Description          = categoryModel.Description;
                    category.Image                = categoryModel.Image;
                    category.LastModificationTime = DateTime.UtcNow;
                    category.CategoryType         = categoryModel.CategoryType;

                    _context.Update(category);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryModelExists(categoryModel.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(categoryModel));
        }