içinde rolleri ile kullanıcı olsun Mvc içinde sadece Role kullanıcılarıalmak istiyorum, ben her şeyi denedim. Bildiğiniz gibi AspNetUserRoles
bir haritalama tablosudur (çoktan çoğa), bu nedenle EDMX'te üretilmez (tasarıma göre, birincil anahtarı yoktur). Ayrıca Rol tablosu (?) kullanamıyoruz. Lütfen benim modelimi göz önünde bulundurun. İşte sadece mvc 5
public async Task<ActionResult> Roles()
{
var list = context.Users.Include(u => u.Roles);
var user = new List<ApplicationUser>();
foreach(var u in list)
{
if(u.Roles!= null)
{
user.Add(u);
}
}
return View(user);
}
ve burada benim görünümdür:
@model IEnumerable<Site.Models.ApplicationUser>
@{
Layout = null;
}
<h2>List of users with Roles:</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Roles)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<th>
@Html.DisplayFor(modelItem => item.Roles)
</th>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
Peki sorun nedir? Kullanıcılara herhangi bir rol almıyor musunuz? – Marius
Sadece Role kullanıcıları almak istiyorum ama bu kod tüm kullanıcıları olsun. – V47
Ya eğer (u.Roles! = Null) ''ı (if (u.Roles.Any())' olarak değiştirirseniz? – Hendry