Last active
April 26, 2018 08:50
-
-
Save Ibro/1803ba060a2f7f5a21c9429fe20fdbdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<IActionResult> Create([Bind("Id,Name,UserId")] Category category) | |
{ | |
var authorizationResult = await _authorizationService.AuthorizeAsync(User, category, new InsertCategoryRequirement()); | |
if (!authorizationResult.Succeeded) // If user has no access show him 403 | |
{ | |
return new ForbidResult(); | |
} | |
ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", category.UserId); | |
// If ModelState isn't valid, return to view immediately | |
if (!ModelState.IsValid) | |
{ | |
return View(category); | |
} | |
// Add category, save changes and redirect to Category/Index - Categories List page | |
_context.Add(category); | |
await _context.SaveChangesAsync(); | |
return RedirectToAction(nameof(Index)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment