Open HeathcliffEarnshaw opened 6 years ago
Comment ko nalang ung ikalawa sa viewmodel naman
ViewModels/UsersViewModel.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using PVMTrading_v1.Models;
namespace PVMTrading_v1.ViewModels
{
public class UsersViewModel
{
//quote ko muna code mo
//public ApplicationUser Users { get; set; }
//public List
//let me try this
public string UserId { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Role { get; set; }
}
}
tapos sa ikatlo sa controller naman
Controllers/UsersController.cs
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using System.Web.Mvc; using PVMTrading_v1.Models; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using Microsoft.AspNet.Identity.EntityFramework;
namespace PVMTrading_v1.Controllers {
public class UsersController : Controller
{
private ApplicationDbContext _application;
public UsersController(ApplicationDbContext application)
{
_application = application;
}
protected override void Dispose(bool disposing)
{
_application.Dispose();
}
// GET: Users
public ActionResult Index()
{
//var userList = _application.Users.ToList();
//return View(userList);
//sweetie, may comment ung tutorial
//context.Users represents the table AspNetUsers which has navigation
//property roles which represent the AspNetUserInRoles table.
//diko alam anong name ng userstable
var usersWithRoles = (from user in _context.Users
select new
{
UserId = user.Id,
Username = user.UserName,
Email = user.Email,
RoleNames = (from userRole in user.Roles
join role in _context.RoleName on userRole.RoleId
equals role.Id
select role.Name).ToList()
}).ToList().Select(p => new UsersViewModel()
{
UserId = p.UserId,
Username = p.Username,
Email = p.Email,
Role = string.Join(",", p.RoleNames)
});
return View(usersWithRoles);
}
}
}
Ganito kasi nangyari bali ginamit ko tutorial, ung link below http://www.c-sharpcorner.com/article/list-of-users-with-roles-in-mvc-asp-net-identity/
Tapos,
Bali confusing sakin ung sa ViewModel, yang code sa baba. Ang intindi ko sa UserViewModel, combination na ng UsersWithRoles no? kaya naka define ung role sa UserViewModel, siguro naming convention lang, dapat ata UserWithRoles ung name, anyway
public string Role { get; set; }
Next pumunta ako sa controller, tignan mo ung code sa baba, may join dito, sabi sa tutorial ung context.Users daw ay definition ng AspNetUsers tapos ung context.Role ay Roles kaya naman nilagay ko nalang ung RoleName na nakita ko sa Model mo, hm, di ako sure pero mag join ka lang naman gamit ung role.Id na meron sa Users Table.
Naisip ko may problem ka sa joining diba, ganito concept lang naman, sample ung sa baba
`from product in db.Products` -> eto ung table A
`join category in db.Categories` -> eto table b
`on product.CategoryId equals category.CategoryId` -> eto ung nagdudugtong sa kanilang dalawa
siguro kung isusulat ko ung query based sa format ng products sa taas, malamang ganito
`from user in db.Users` -> eto ung table A sa users, AspNetUsers daw
`join role in db.RoleNames` -> eto table b ung RoleName table mo
`on user.RoleId equals role.Id` -> eto ung nagdudugtong sa kanilang dalawa`
p.s.
diko alam tawagin ung users table ng asp.net sleepy na ko sweetie, sleep muna ako ha
sweetie, bali tatlong file lng pinakelaman ko sa userViews wala kasi akong push access
Views/Users/Index.cshtml
@model /List / IEnumerable