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

parent 5d3a488e
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using webinar65.Models;
using webinar65.ViewModels;
namespace webinar65.Controllers
......
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using webinar65.Models;
using webinar65.ViewModels;
namespace webinar65.Controllers
{
public class UserController : Controller
{
private UserManager<User> _userManager;
private RoleManager<IdentityRole> _roleManager;
public UserController(UserManager<User> userManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
public IActionResult Index()
{
return View(_userManager.Users);
}
[HttpGet]
public IActionResult Create()
{
return View(new UserCreateViewModel(_roleManager.Roles.ToList()));
}
[HttpPost]
public async Task<IActionResult> Create(UserCreateViewModel model)
{
if (ModelState.IsValid)
{
var newUser = new User
{
UserName = model.Username,
Email = model.Email
};
var result = await _userManager.CreateAsync(newUser, model.Password);
if (result.Succeeded)
return RedirectToAction("Index");
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
return View(model);
}
}
}
using System.Collections.Generic;
using Microsoft.AspNetCore.Identity;
namespace webinar65.Models
......
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Rendering;
using webinar65.Models;
namespace webinar65.ViewModels
......@@ -16,6 +18,23 @@ namespace webinar65.ViewModels
[Required]
public string Password { get; set; }
public List<IdentityRole> Roles { get; set; }
[Required]
public string RoleName { get; set; }
public List<SelectListItem> Roles { get; set; }
public UserCreateViewModel()
{
}
public UserCreateViewModel(List<IdentityRole> roles)
{
Roles = roles.Select(r => new SelectListItem
{
Text = r.Name,
Value = r.Name
}).ToList();
}
}
}
......@@ -31,6 +31,7 @@
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Role" asp-action="Index">Roles</a></li>
<li><a asp-area="" asp-controller="User" asp-action="Index">Users</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="Contact">Contact</a></li>
......
@model webinar65.ViewModels.UserCreateViewModel
@{
ViewBag.Title = "Create User";
Layout = "_Layout";
}
<h2>Create User</h2>
<div asp-validation-summary="All" class="text-danger"></div>
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="Email">Email</label>
<input asp-for="Email" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="Username">Username</label>
<input asp-for="Username" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="Password">Password</label>
<input asp-for="Password" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="RoleName">RoleName</label>
<select asp-for="RoleName" asp-items="@Model.Roles"></select>
</div>
<button type="submit" class="btn btn-primary">Create</button>
<a asp-action="Index" class="btn btn-secondary">Cancel</a>
</form>
@model IEnumerable<webinar65.Models.User>
@{
ViewBag.Title = "Users";
Layout = "_Layout";
}
<h2>Users</h2>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">User name</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
@if (!Model.Any())
{
<tr>
<td colspan="3" class="text-center">No Users</td>
</tr>
}
else
{
foreach (User user in Model)
{
<tr>
<td>@user.Id</td>
<td>@user.UserName</td>
<td>@user.Email</td>
</tr>
}
}
</tbody>
</table>
<a asp-action="Create" class="btn btn-primary">Create</a>
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