Добавил контроллер для работы с ролями

parent f0f5322d
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using webinar65.Models;
using webinar65.ViewModels;
namespace webinar65.Controllers
{
public class RoleController : Controller
{
private RoleManager<IdentityRole> _roleManager;
public RoleController(RoleManager<IdentityRole> roleManager)
{
_roleManager = roleManager;
}
public IActionResult Index()
{
return View(_roleManager.Roles);
}
[HttpGet]
public IActionResult Create() => View();
[HttpPost]
public async Task<IActionResult> Create(RoleCreateViewModel model)
{
if (ModelState.IsValid)
{
var result = await _roleManager.CreateAsync(new IdentityRole(model.RoleName));
if (result.Succeeded)
return RedirectToAction("Index");
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
return View(model);
}
}
}
...@@ -61,6 +61,7 @@ namespace webinar65 ...@@ -61,6 +61,7 @@ namespace webinar65
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseCookiePolicy(); app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes => app.UseMvc(routes =>
{ {
......
using System.ComponentModel.DataAnnotations;
namespace webinar65.ViewModels
{
public class RoleCreateViewModel
{
[Required]
public string RoleName { get; set; }
}
}
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Identity;
using webinar65.Models;
namespace webinar65.ViewModels
{
public class UserCreateViewModel
{
[Required]
public string Username { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string Password { get; set; }
public List<IdentityRole> Roles { get; set; }
}
}
@model webinar65.ViewModels.RoleCreateViewModel
@{
ViewBag.Title = "Create Role";
Layout = "_Layout";
}
<h2>Create Role</h2>
<div asp-validation-summary="All" class="text-danger"></div>
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="RoleName">Role Name</label>
<input asp-for="RoleName" class="form-control"/>
</div>
<button type="submit" class="btn btn-primary">Create</button>
<a asp-action="Index" class="btn btn-secondary">Cancel</a>
</form>
@using Microsoft.AspNetCore.Identity
@model IEnumerable<Microsoft.AspNetCore.Identity.IdentityRole>
@{
ViewBag.Title = "Roles";
Layout = "_Layout";
}
<h2>Roles</h2>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Role name</th>
</tr>
</thead>
<tbody>
@if (!Model.Any())
{
<tr>
<td colspan="3" class="text-center">No Roles</td>
</tr>
}
else
{
foreach (IdentityRole role in Model)
{
<tr>
<td>@role.Id</td>
<td>@role.Name</td>
</tr>
}
}
</tbody>
</table>
<a asp-action="Create" class="btn btn-primary">Create</a>
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
</div> </div>
<div class="navbar-collapse collapse"> <div class="navbar-collapse collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Role" asp-action="Index">Roles</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li> <li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li> <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li> <li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment