[英]Nopcommerce Data Access Layer
在我的〜/ Presentation / Nop.Web / Controllers / ProductController.cs中,我創建了一種使用googleMapsApi進行過濾的方法,如下所示:
// GET: /Product/StoreLocatorResults
public ActionResult StoreLocatorResults(string address)
{
// Make sure we have an address - if not, send user back to /Product/StoreLocator
if (string.IsNullOrEmpty(address))
return RedirectToAction("StoreLocator");
// Get the lat/long info about the address
var results = GoogleMapsAPIHelpersCS.GetGeocodingSearchResults(address);
// Determine the lat & long parameters
var lat = Convert.ToDecimal(results.Element("result").Element("geometry").Element("location").Element("lat").Value, NumberFormatInfo.InvariantInfo);
var lng = Convert.ToDecimal(results.Element("result").Element("geometry").Element("location").Element("lng").Value, NumberFormatInfo.InvariantInfo);
// var vendors = _vendorService.GetAllVendors();
// Get those locations near the store
TyrePromosContext context = new TyrePromosContext();
var nearbyStores = from store in context.contextVendors
where Math.Abs(decimal.Parse(store.Latitude) - lat) < 0.25M &&
Math.Abs(decimal.Parse(store.Longitude) - lng) < 0.25M
select new NearbyStoreLocation()
{
Id = store.Id,
VendorAddress = store.VendorAddress,
City = store.City,
PostalCode = store.PostalCode,
Latitude = store.Latitude,
Longitude = store.Longitude,
AddressLatitude = lat,
AddressLongitude = lng
};
// Order the results from nearest to farthest
var nearbySortedStores = nearbyStores.ToList().OrderBy(s => s.DistanceFromAddress).ToList();
return View(nearbySortedStores);
}
我創建了一個名為TyrePromosContext的上下文,該上下文似乎連接良好,因為我沒有遇到任何構建錯誤,並且Web項目運行正常,但是當在上面的StoreLocatorResults方法中解析了地址字符串時,我收到了此錯誤消息
錯誤消息:EntityFramework.dll中發生類型為'System.Data.SqlClient.SqlException'的異常,但未在用戶代碼中處理
附加信息:建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱正確和SQL
現在我的問題是如何使用服務層以這種方式獲取所有供應商:
var vendors = _vendorService.GetAllVendors(model.SearchName, command.Page - 1, command.PageSize, true);
像在〜Presentation / Nop.Web / Administration / Controllers / VendorController.cs中一樣?
要在nopCommerce中使用服務,您必須為_vendor服務添加一個屬性,並將其添加到您的構造函數中,以遵守相關性寄存器,如下所示:
IVendorService _vendor;
public ProductController(IVendorService vendor)
{
_vendor = vendor ;
}
而且,您可以在要添加的_vendor屬性的“所需操作”中使用此服務。
希望對您有所幫助。 最好的祝福。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.