Add or remove users from role in asp net core

preview_player
Показать описание

Text version of the video

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.

Slides

ASP.NET Core Text Articles & Slides

ASP.NET Core Tutorial

Angular, JavaScript, jQuery, Dot Net & SQL Playlists
Рекомендации по теме
Комментарии
Автор

Absolutely the best tutorials out there. Ever. Full stop. No question. There are none better.

MB-nwsz
Автор

Excellent Videos!! learning alot.

Did notice that the result.Succeeded check at the bottom of the loop is not needed. also there is no reaction to if result does not succeed. i realise with the if statements that it would never fail however for the pedantic people out there:

I added a validation summary div above the card footer div and changed the result.Suceeded if statement at the bottom of the loop to pickup any failures and add messages to the ModelState. Then as there is a failure I wanted the user not to redirect away but show this view with actual information so the user can try again.. so I corrected the IsSelected for the failed item.

Like so:


<div class="card-body">

@for (int i = 0; i < Model.Count; i++)
{
<div class="form-check m-1">
@Model[i].UserName
</label>
</div>
}
</div>
<div asp-validation-summary="All" class="text-danger"></div>
<div class="card-footer">
<input type="submit" class="btn btn-primary" value="Update" />
</div>


and instead of the result.Succeeded if statement at the bottom of the loop..




if (!result.Succeeded)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, $"User '{user.UserName}' : {error.Description}");
model[i].IsSelected = await this.userManager.IsInRoleAsync(user, role.Name);
}
if (i >= (model.Count - 1))
{
return View(model);
}
}

Hope this satisfies the pedantic! :)

canasjoe
Автор

This dude is the GOAT...absolute best tutorials on

donnyjames
Автор

This is the best on the internet... Thank you

Sam-ybut
Автор

Thank you so much kudvenkat, I'm happy I found this resource, it's gold.

seetheious
Автор

Fantastic to say the least. I have learnt a ton. Thank you so much!!!

nicon
Автор

FYI: If you use, foreach in the EditUsersInRole view, it won't work properly. The input-id tag won't be different, clicking won't work properly, and it will return no list in the post request. Thank youu

anamuslimun
Автор

It is really amazing. To get all this from the best man for free.

naodagere
Автор

Had an error with the foreach loop in the code.
Saying datareader need to be closed, fixed it by adding to my connecting string in appsettings.json

It looks like this now: "EmployeeDBConnection":


Dont know if its only in ASP.NET core 3 its an error

krutelut
Автор

The video is great as usual keep it up bro.
I'm working with ASP(3.1) and i encountered a problem:
i have chose 2 users in a role and checked them out in the db they were there, , but when i come back to the "/Administration/EditRole/" to uncheck or check other, , I found all the checkboxs are unchecked already

ahmadkarem
Автор

If I block this portion of the code, the functionality is still intact:

if (result.Succeeded)
{
if (i < model.Count - 1)
continue;
else
return RedirectToAction("EditRole", new { Id = role.Id });
}

Anybody found otherwise. I am trying to slice and dice from all possible aspects.
Great content as always. Sometimes it feels overwhelming when presented with so much knowledge at one shot.

amitghosh
Автор

hi Venkat, I have a question:
In the view you use a for-loop. I tried myself before watching you, and choiced a foreach loop, but the post method received an empty list as the model ...
Why? what's the difference between for and foreach loop? I always thought, they are same, as long as you don't need the indexes.

CoderboyPB
Автор

Awesome, Many thanks for you great video tutorial.

rayt
Автор

Error open DataReader Solution:

For anyone experiencing an error with their DataReader solution already being open the problem is that "userManager.Users" is not asynchronous. One solution to this is to extend it with the following: be sure to #include in order to access this extension method.

CODE FROM VIDEO WITH ERROR
//foreach (var user in userManager.Users)
//{
// if (await userManager.IsInRoleAsync(user, role.Name))
// {
//
// }
//}

NEW CODE WITH EXTENSION
//foreach (var user in await
//{
// if (await userManager.IsInRoleAsync(user, role.Name))
// {
//
// }
//}

A proposed solution for some is to add to their connection string however, if you're using MySQL Server instead of SQLServer this will not be available.

Dugz
Автор

Hi Venkat, thanks for the awesome tutorial,
Just wanted to check how is roleId is passed from the view to the controller as we didn't specify it as a routing parameter in the submit button, or did the model binder could bind the value to the parameter in the action method?

juankasem
Автор

Thank you for a video. Could you please explain why do we need this code:


if (result.Succeeded)
{
if (i < (model.Count - 1))
continue;
else
return RedirectToAction("EditRole", new { Id = roleId });
}


If remove that part we will have same functionality, loop in "for" loop and RedirectToAction("EditRole", new { Id = roleId });
after loop

sakawduentookaew
Автор

In EditUsersInRole View if I change for loop to foreach I dont get any information to List<UserRoleViewModel> model. Please could you explain me why it is?


@foreach (var user in Model)
{
<div class="form-check m-1">
@user.UserName
</label>
</div>
}

etNictinQ
Автор

Hi Venkat, what if i am not using the rolemanager built in class, how do i access the userInRole(),

chomuekez
Автор

hi venkat i have one doubt, how can we determine result = await userManager.AddToRoleAsync(user, role.Name); method is used to add the rows to this ' AspNetUserRoles' table by viewing in code only? . In the this video you identified and showed the 'bridge table' directly.... how could we know that in code on which table this AddToRoleAsync method going to insert the rows without going to see the tables directly in sql server object explorer. am waiting for your reply ;)

pavankogpayana
Автор

Dear Kudvenkat! Very thanks for your video! Could you explain please, why are there no any users in "Users in this role" section on "EditRole" post method?

pavelklimov
join shbcf.ru