public ActionResult SaveMapAjax(int id, string mapData)
        {
            var model = new TerritoryMapSavedModel();

            try
            {
                using (var dataContext = new HuntingEntities())
                {
                    var user = AclUserContext.GetDetail(dataContext, User.Identity.Name);
                    if (user == null)
                    {
                        ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED);
                        return(PartialView(model));
                    }
                    var territory = TerritoryContext.GetDetail(dataContext, id);
                    if (user.CanUpdateTerritory(territory) == false)
                    {
                        ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED);
                        return(PartialView(model));
                    }

                    var saveModel = JsonConvert.DeserializeObject <MapSaveDataModel>(mapData);
                    model = TerritoryContext.SaveMap(dataContext, id, saveModel, user.Id);
                    return(PartialView(model));
                }
            }
            catch (Exception exception)
            {
                logger.Error(exception, "TerritoryController");
                ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION);
                return(PartialView(model));
            }
        }
        public static TerritoryMapSavedModel SaveMap(HuntingEntities dataContext, int territoryId, MapSaveDataModel model, int userId)
        {
            var saveModel   = new TerritoryMapSavedModel();
            var mapAreaList = new List <MapArea>();
            var mapItemList = new List <MapItem>();

            try
            {
                var territory = dataContext.Territories.First(item => item.Id == territoryId);
                foreach (var mapAreaModel in model.MapAreaList)
                {
                    MapArea mapArea;
                    if (mapAreaModel.Id > 0)
                    {
                        mapArea = dataContext.MapAreas.First(item => item.Id == mapAreaModel.Id);
                    }
                    else
                    {
                        mapArea = new MapArea()
                        {
                            SysCreated  = DateTime.Now,
                            SysCreator  = userId,
                            TerritoryId = territoryId,
                        };
                        dataContext.MapAreas.Add(mapArea);
                    }
                    mapArea.Name        = HttpUtility.HtmlDecode(mapAreaModel.Title);
                    mapArea.Description = HttpUtility.HtmlDecode(mapAreaModel.Description);
                    mapArea.PolygonData = string.Join(",", mapAreaModel.Polygon);
                    mapArea.IsDeleted   = mapAreaModel.IsDeleted;
                    mapArea.SysEditor   = userId;
                    mapArea.SysUpdated  = DateTime.Now;
                    mapAreaList.Add(mapArea);
                }
                foreach (var mapItemModel in model.MapItemList)
                {
                    MapItem mapItem;
                    if (mapItemModel.Id > 0)
                    {
                        mapItem = dataContext.MapItems.First(item => item.Id == mapItemModel.Id);
                    }
                    else
                    {
                        mapItem = new MapItem()
                        {
                            MapItemType = mapItemModel.ItemType,
                            SysCreated  = DateTime.Now,
                            SysCreator  = userId,
                            TerritoryId = territoryId,
                        };
                        dataContext.MapItems.Add(mapItem);
                    }
                    mapItem.Name        = HttpUtility.HtmlDecode(mapItemModel.Title);
                    mapItem.Description = HttpUtility.HtmlDecode(mapItemModel.Description);
                    mapItem.LocationX   = mapItemModel.Position.Lat;
                    mapItem.LocationY   = mapItemModel.Position.Lng;
                    mapItem.IsDeleted   = mapItemModel.IsDeleted;
                    mapItem.SysEditor   = userId;
                    mapItem.SysUpdated  = DateTime.Now;
                    mapItemList.Add(mapItem);
                }
                dataContext.SaveChanges();
                saveModel.MapAreaIdList = mapAreaList.ConvertAll(item => item.Id);
                saveModel.MapItemIdList = mapItemList.ConvertAll(item => item.Id);
                saveModel.IsSuccess     = true;
            }
            catch (Exception exception)
            {
                logger.Error(exception, "SaveMap");
            }
            return(saveModel);
        }