public List <ShopViewModel> GetAllShops() { List <ShopModel> shops = shopRepository.GetAllShopswithName(); var categories = categoryRepository.GetAllCategories(); List <ShopViewModel> shopViewModels = new List <ShopViewModel>(); foreach (var shop in shops) { var shopViewModel = new ShopViewModel() { Name = shop.Name, Email = shop.Email, Phone = shop.Phone, ShopId = shop.ShopId, CategoryId = shop.CategoryID }; foreach (var category in categories) { if (category.CategoryID == shop.CategoryID) { shopViewModel.CategoryName = category.Name; } } var address = addressRepository.GetAddressByID(shop.AddressID); string addressDetails = $"{address.StreetNumber}, {address.Street}, {address.PostCode}, {address.County}, {address.City}, {address.Country}"; shopViewModel.Address = addressDetails; var paymentMethods = paymentMethodsRepository.GetAllPaymentMethodsByShopId(shop.ShopId); shopViewModel.PaymentMethods = string.Join(", ", paymentMethods.Select(x => x.Name)); //foreach (var paymentMethod in paymentMethods) //{ // shopViewModel.PaymentMethods += $"{paymentMethod.Name},"; //} shopViewModels.Add(shopViewModel); } return(shopViewModels); }
// GET: Shop/Edit/5 public ActionResult Edit(int id) { Models.ShopModel shopModel = shopRepository.GetShopByID(id); var shopViewModel = new ShopEditViewModel(); shopViewModel.Shop = shopModel; var categories = categoryRepository.GetAllCategories(); SelectList categoryList = new SelectList(categories, "CategoryID", "Name"); ViewData["CategoryList"] = categoryList; var address = addressRepository.GetAddressByID(shopModel.AddressID); shopViewModel.Address = address; var allPaymentMethods = paymentMethodsRepository.GetAllPaymentMethods(); ViewData["AllPaymentMethods"] = allPaymentMethods; var shopPaymentMethods = paymentMethodsRepository.GetAllPaymentMethodsByShopId(shopModel.ShopId); List <PaymentMethodViewModel> paymentMethodViewModels = new List <PaymentMethodViewModel>(); foreach (var paymentMethodModel in allPaymentMethods) { var paymentMethodViewModel = new PaymentMethodViewModel(); paymentMethodViewModel.PaymentMethod = paymentMethodModel; foreach (var shopPaymentMethod in shopPaymentMethods) { if (shopPaymentMethod.PaymentMethodID == paymentMethodModel.PaymentMethodID) { paymentMethodViewModel.Selected = true; } } paymentMethodViewModels.Add(paymentMethodViewModel); } shopViewModel.PaymentMethods = paymentMethodViewModels; return(View("EditShop", shopViewModel)); }