Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
W
webinar65
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Нурбек Созоев
webinar65
Commits
2040f456
Commit
2040f456
authored
Jun 27, 2019
by
Нурбек Созоев
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Добавил контроллер для работы с пользователями
parent
5d3a488e
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
153 additions
and
2 deletions
+153
-2
RoleController.cs
webinar65/Controllers/RoleController.cs
+0
-1
UserController.cs
webinar65/Controllers/UserController.cs
+58
-0
User.cs
webinar65/Models/User.cs
+1
-0
UserCreateViewModel.cs
webinar65/ViewModels/UserCreateViewModel.cs
+20
-1
_Layout.cshtml
webinar65/Views/Shared/_Layout.cshtml
+1
-0
Create.cshtml
webinar65/Views/User/Create.cshtml
+34
-0
Index.cshtml
webinar65/Views/User/Index.cshtml
+39
-0
No files found.
webinar65/Controllers/RoleController.cs
View file @
2040f456
using
System.Threading.Tasks
;
using
Microsoft.AspNetCore.Identity
;
using
Microsoft.AspNetCore.Mvc
;
using
webinar65.Models
;
using
webinar65.ViewModels
;
namespace
webinar65.Controllers
...
...
webinar65/Controllers/UserController.cs
0 → 100644
View file @
2040f456
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
);
}
}
}
webinar65/Models/User.cs
View file @
2040f456
using
System.Collections.Generic
;
using
Microsoft.AspNetCore.Identity
;
namespace
webinar65.Models
...
...
webinar65/ViewModels/UserCreateViewModel.cs
View file @
2040f456
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
();
}
}
}
webinar65/Views/Shared/_Layout.cshtml
View file @
2040f456
...
...
@@ -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>
...
...
webinar65/Views/User/Create.cshtml
0 → 100644
View file @
2040f456
@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>
webinar65/Views/User/Index.cshtml
0 → 100644
View file @
2040f456
@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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment