Instantly share code, notes, and snippets.

Embed
What would you like to do?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,UserId")] Category category)
{
// Populate UserId select list.
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