payawalangelica / PVMTrading_v2

0 stars 0 forks source link

Changed file #1

Open HeathcliffEarnshaw opened 6 years ago

HeathcliffEarnshaw commented 6 years ago

sweetie, bali tatlong file lng pinakelaman ko sa userViews wala kasi akong push access

Views/Users/Index.cshtml

@model /List/ IEnumerable

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@*<h2>Index</h2>
<table class="table">
    <tr>
        <th>
            Id
        </th>
        <th>
            Email
        </th>
        <th>
            UserName
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.BranchId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UserName)
            </td>

        </tr>
    }
</table>*@
@* Di ko alam ung mga pinag-gagawa ko, di kita m-call sweetie
    pero eto ung link nung tutorial
    http://www.c-sharpcorner.com/article/list-of-users-with-roles-in-mvc-asp-net-identity/
    *@
<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="box-title">
            <b>Users with Roles</b>
        </h3>
    </div>
    <!-- /.box-header -->
    <div class="panel-body">
        <table class="table table-hover table-bordered table-condensed" id="UsersWithRoles">
            <thead>
                <tr>
                    <td><b>Username</b></td>
                    <td><b>Email</b></td>
                    <td><b>Roles</b></td>
                </tr>
            </thead>
            @foreach (var user in Model)
            {
                <tr>
                    <td>@user.Username</td>
                    <td>@user.Email</td>
                    <td>@user.Role</td>

                </tr>
            }
        </table>
    </div>

    <div class="panel-footer">
        <p class="box-title"><b>Total Users till @String.Format("{0 : dddd, MMMM d, yyyy}", DateTime.Now)  : </b><span class="label label-primary">@Model.Count()</span></p>
    </div>
</div>  
HeathcliffEarnshaw commented 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 Roles { get; set; }

    //let me try this
    public string UserId { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public string Role { get; set; }
}

}

HeathcliffEarnshaw commented 6 years ago

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);

    }

}

}

HeathcliffEarnshaw commented 6 years ago

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.

capture

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