Repository navigation
Expand file tree
/
Copy pathCountryController.cs
More file actions
71 lines (58 loc) · 2.58 KB
/
Copy pathCountryController.cs
File metadata and controls
71 lines (58 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Linq;
using System.Web.Mvc;
using SmartStore.Core;
using SmartStore.Core.Caching;
using SmartStore.Services.Directory;
using SmartStore.Services.Localization;
using SmartStore.Web.Infrastructure.Cache;
using SmartStore.Web.Framework.Controllers;
namespace SmartStore.Web.Controllers
{
public partial class CountryController : PublicControllerBase
{
#region Fields
private readonly ICountryService _countryService;
private readonly IStateProvinceService _stateProvinceService;
private readonly ILocalizationService _localizationService;
private readonly IWorkContext _workContext;
private readonly ICacheManager _cacheManager;
#endregion
#region Constructors
public CountryController(ICountryService countryService,
IStateProvinceService stateProvinceService,
ILocalizationService localizationService,
IWorkContext workContext,
ICacheManager cacheManager)
{
this._countryService = countryService;
this._stateProvinceService = stateProvinceService;
this._localizationService = localizationService;
this._workContext = workContext;
this._cacheManager = cacheManager;
}
#endregion
#region States / provinces
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetStatesByCountryId(string countryId, bool addEmptyStateIfRequired)
{
//this action method gets called via an ajax request
if (String.IsNullOrEmpty(countryId))
throw new ArgumentNullException("countryId");
string cacheKey = string.Format(ModelCacheEventConsumer.STATEPROVINCES_BY_COUNTRY_MODEL_KEY, countryId, addEmptyStateIfRequired, _workContext.WorkingLanguage.Id);
var cacheModel = _cacheManager.Get(cacheKey, () =>
{
var country = _countryService.GetCountryById(Convert.ToInt32(countryId));
var states = _stateProvinceService.GetStateProvincesByCountryId(country != null ? country.Id : 0).ToList();
var result = (from s in states
select new { id = s.Id, name = s.GetLocalized(x => x.Name) })
.ToList();
if (addEmptyStateIfRequired && result.Count == 0)
result.Insert(0, new { id = 0, name = _localizationService.GetResource("Address.OtherNonUS") });
return result;
});
return Json(cacheModel, JsonRequestBehavior.AllowGet);
}
#endregion
}
}