UserRole javítása
This commit is contained in:
@@ -1,4 +1,108 @@
|
||||
@page
|
||||
@model WorkFlowCheck.Web.Pages.UserAndRole.UserRoleEditPageModel
|
||||
@using Microsoft.AspNetCore.Antiforgery
|
||||
@inject IAntiforgery Antiforgery
|
||||
@{
|
||||
ViewData["Title"] = "Ellenőrzési pont";
|
||||
var UserRoleId = Model.UserRoleDTO.Id;
|
||||
var urlPost = Url.Page("./UserRoleEditPage", "Save");
|
||||
}
|
||||
<h1>@ViewData["Title"]</h1>
|
||||
|
||||
<input type="hidden" id="UserRoleEditPostUrl" value="@urlPost" />
|
||||
|
||||
<div class="card shadow p-4">
|
||||
<form method="post" id="UserRoleForm">
|
||||
<meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
|
||||
<input type="hidden" asp-for="UserRoleDTO.Id" />
|
||||
<div class="mb-3">
|
||||
<div class="col-md-3">
|
||||
<label asp-for="UserRoleDTO.RoleId"></label>
|
||||
<select asp-for="UserRoleDTO.RoleId" class="form-control" asp-items="Model.Roles"></select>
|
||||
<span asp-validation-for="UserRoleDTO.RoleId" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<label asp-for="UserRoleDTO.UserId"></label>
|
||||
<select asp-for="UserRoleDTO.UserId" class="form-control" asp-items="Model.Users"></select>
|
||||
<span asp-validation-for="UserRoleDTO.UserId" class="text-danger"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 d-flex justify-content-between">
|
||||
<button type="button" id="saveUserRole" class="btn btn-primary">Mentés</button>
|
||||
<button type="button" class="btn btn-secondary" onclick="history.back()">Mégsem</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
<script>
|
||||
|
||||
$("#saveUserRole").click(function () {
|
||||
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
||||
var formData = getFormAsNestedObject('#UserRoleForm');
|
||||
const $form = $('#UserRoleForm');
|
||||
|
||||
if ($form.valid())
|
||||
{
|
||||
$.ajax({
|
||||
url: $('#UserRoleEditPostUrl').val(),
|
||||
type: 'POST',
|
||||
headers: {
|
||||
'X-CSRF-TOKEN': csrfToken,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: JSON.stringify(formData.UserRoleDTO),
|
||||
success: function (response) {
|
||||
// console.log(response);
|
||||
if (response.success)
|
||||
{
|
||||
showMessageModal({
|
||||
title: 'Figyelmem!',
|
||||
message: 'Sikeres mentés.',
|
||||
okText: 'Értettem'
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
showMessageModal({
|
||||
title: 'Figyelmem, HIBA!',
|
||||
message: 'Sikertelen mentés.',
|
||||
okText: 'Értettem'
|
||||
});
|
||||
}
|
||||
},
|
||||
error: function (xhr, status, error) {
|
||||
// Hiba esetén
|
||||
console.error("Hiba: ", error);
|
||||
showMessageModal({
|
||||
title: 'Hiba!',
|
||||
message: 'A mentés NEM sikerült!',
|
||||
okText: 'Értettem'
|
||||
});
|
||||
}
|
||||
});
|
||||
} else
|
||||
{
|
||||
$form[0].reportValidity();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
$(document).ready(function () {
|
||||
const $form = $("#UserRoleForm");
|
||||
|
||||
$form.validate({
|
||||
errorClass: "text-danger small",
|
||||
errorPlacement: function (error, element) {
|
||||
error.appendTo(element.parent());
|
||||
}
|
||||
});
|
||||
|
||||
// $form.on("submit", function (e) {
|
||||
// if (!$form.valid()) {
|
||||
// e.preventDefault(); // Ne küldje be, ha van hiba
|
||||
// }
|
||||
// });
|
||||
});
|
||||
</script>
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Serilog;
|
||||
using WorkFlowCheck.Common.DTO;
|
||||
using WorkFlowCheck.Web.Services.Interfaces;
|
||||
@@ -13,7 +14,11 @@ namespace WorkFlowCheck.Web.Pages.UserAndRole
|
||||
|
||||
[BindProperty]
|
||||
public UserRoleDTO UserRoleDTO { get; set; }
|
||||
|
||||
[BindProperty]
|
||||
public List<SelectListItem> Roles { get; set; }
|
||||
[BindProperty]
|
||||
public List<SelectListItem> Users { get; set; }
|
||||
|
||||
public UserRoleEditPageModel(ILogger<IndexModel> logger, IUserService userService)
|
||||
{
|
||||
_logger = logger;
|
||||
@@ -22,6 +27,7 @@ namespace WorkFlowCheck.Web.Pages.UserAndRole
|
||||
|
||||
public async Task OnGet(int id)
|
||||
{
|
||||
await InitSelectItems();
|
||||
UserRoleDTO = await _userService.GetUserRole(id);
|
||||
}
|
||||
public async Task<IActionResult> OnPostSave([FromBody] UserRoleDTO userRoleDTO)
|
||||
@@ -45,5 +51,20 @@ namespace WorkFlowCheck.Web.Pages.UserAndRole
|
||||
|
||||
return new JsonResult(new { success = false });
|
||||
}
|
||||
private async Task InitSelectItems()
|
||||
{
|
||||
var roles = await _userService.GetAllRoles();
|
||||
this.Roles = new List<SelectListItem>();
|
||||
foreach (var role in roles)
|
||||
{
|
||||
this.Roles.Add(new SelectListItem() { Value = role.Id.ToString(), Text = role.RoleName });
|
||||
}
|
||||
var users = await _userService.GetAllUsers();
|
||||
this.Users = new List<SelectListItem>();
|
||||
foreach (var user in users)
|
||||
{
|
||||
this.Users.Add(new SelectListItem() { Value = user.Id.ToString(), Text = user.UserName });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user