如何过滤发送到视图的数据根据​​用户登录

时间:2017-03-15 15:16:48

标签: asp.net-mvc

这是我的代码

[Authorize(Roles = "Supplier")]
    public ActionResult Orders()
    {
        if (!String.IsNullOrEmpty(TempData["error"] as string))
            ViewBag.Error = TempData["error"] as string;
        TempData.Clear();

   if (User.IsInRole("Admin"))//when the user logs in as the admin
        {
            var orders = _uow.orderRepo.GetAll().ToList();
            List<Models.Admin.OrderGridModel> model = null;
            model = (from c in orders 
                     select new Models.Admin.OrderGridModel()
                     {
                         OrderNumber = c.OrderNumber,
                         Date = c.Date,
                         Id = c.Id,
                         FullName = c.Person.FirstName + " " + c.Person.LastName,
                         PhoneNr = c.Person.Tel,
                         Email = c.Person.Email,
                         Price = BLL.PriceFormatter.FormatPrice(c.Price, c.Currency),
                         IsPaid = c.Zaplaceno,
                         Activities = (from a in c.OrderActivities
                                       select new Models.Admin.OrderGridActivityModel()
                                       {

                                           Id = a.Id,
                                           Time = a.Time,
                                           Name = a.Activity.Name,
                                           Date = a.Date,
                                           Status = a.Status,
                                           Supplier = a.Activity.Supplier != null ? a.Activity.Supplier.Name : "",
                                           BookingRefNr = a.SupplierRefNr,
                                           Hotel = c.Hotelname,
                                           Price = BLL.PriceFormatter.FormatPrice(a.Price, a.Order.Currency),
                                           PriceTypes = BLL.PriceFormatter.GetPriceTypesToString(a.OrderPrices),
                                           voucher_ext = a.VoucherFileExtension,
                                           voucher_name = a.VoucherFileName,
                                           voucher_type = a.VoucherFileContentype,

                                       }).ToList()
                     }).ToList();
            return View(model);
        }
        if (User.IsInRole("Supplier"))// when the user logs in as the supplier
        {
            //code for filtering according to the supplier 

        }

        return View();

    }

我从数据库获取订单,但我希望在供应商登录时过滤这些订单,只会返回供应商名称根据登录信息的记录

1 个答案:

答案 0 :(得分:0)

在您的存储库中,您有这个

_uow.orderRepo.GetAll();

创建另一个按供应商ID过滤的商品

public IList<Order> GetSupplier(string id)
{
    return context.Orders.Where(o => o.User.Id = id).ToList();
}

然后你可以这样做:

_uow.orderRepo.GetSupplier(User.Identity.GetUserId());

User.Identity.GetUserId()是登录用户。

相关问题