public JsonResult SaveTrip(TripModel model, string points, DateTime dateAt, DateTime? dateTo, string costPerRoute)
        {
            var currentUser = _usersService.GetUserByLogin(System.Web.HttpContext.Current.User.Identity.Name);
            var trip = new Trip
                {
                    OwnerId = currentUser.Id
                };

            var costs = costPerRoute.Split(';');

            // Дисериализуем строку с массивом точек
            var js = new JavaScriptSerializer();
            var deserializedPoints = (object[])js.DeserializeObject(points);
            var myPoints = new List<SJSonModel>();

            if (deserializedPoints != null)
            {
                // получаем массив точек
                myPoints.AddRange(from Dictionary<string, object> newFeature in deserializedPoints select new SJSonModel(newFeature));

                var mainRouteStr = new List<string>();
                var mainRouteShortStr = new List<string>();

                foreach (var point in myPoints)
                {
                    mainRouteStr.Add(point.FullName);
                    mainRouteShortStr.Add(point.ShortName);
                }

                trip.MainRouteStr = string.Join(";", mainRouteStr);
                trip.MainRouteShortStr = string.Join(";", mainRouteShortStr);
            }

            if (model.IsDriver)
            {
                trip.TripType = TripType.Driver;

                // генерируем всевозможные варианты маршрутов по этому пути точек
                for (var i = 0; i < myPoints.Count - 1; i++)
                {
                    // счетчик для стоимости маршрута
                    var xCost = Convert.ToInt32(costs[i]);
                    for (var j = i + 1; j < myPoints.Count; j++)
                    {
                        var route = new TripRoute
                        {
                            StartPointGid = myPoints[i].Gid,
                            StartPointFullName = myPoints[i].FullName,
                            StartPointShortName = myPoints[i].ShortName,
                            EndPointGid = myPoints[j].Gid,
                            EndPointFullName = myPoints[j].FullName,
                            EndPointShortName = myPoints[j].ShortName,
                            Cost = xCost
                        };

                        trip.TripRoute.Add(route);

                        if (j != myPoints.Count - 1)
                        {
                            xCost += Convert.ToInt32(costs[j]);
                        }
                    }
                }
            }
            else
            {
                trip.TripType = TripType.Passenger;

                // генерируем маршрут только из точки А в точку В
                trip.TripRoute.Add(new TripRoute
                    {
                        StartPointGid = myPoints.First().Gid,
                        StartPointFullName = myPoints.First().FullName,
                        StartPointShortName = myPoints.First().ShortName,
                        EndPointGid = myPoints.Last().Gid,
                        EndPointFullName = myPoints.Last().FullName,
                        EndPointShortName = myPoints.Last().ShortName,
                        Cost = 0
                    });
            }

            if (dateTo == null || model.IsDriver)
            {
                trip.TripDate.Add(new TripDate
                {
                    Date = dateAt,
                    IsDeleted = false
                });
            }
            else
            {
                for (var date = dateAt; date <= dateTo.Value; date = date.AddDays(1))
                {
                    trip.TripDate.Add(new TripDate
                    {
                        Date = date,
                        IsDeleted = false
                    });
                }
            }

            trip.Seats = model.SeatsNumber;
            trip.FreeSeats = model.SeatsNumber;
            trip.TripStatus = 1;
            trip.IsDeleted = false;

            _tripsService.Insert(trip);
            _tripsService.Save();

            return Json(true, JsonRequestBehavior.AllowGet);
        }
 /// <summary>
 /// Создание нового объекта TripRoute.
 /// </summary>
 /// <param name="id">Исходное значение свойства Id.</param>
 /// <param name="tripId">Исходное значение свойства TripId.</param>
 /// <param name="startPointGid">Исходное значение свойства StartPointGid.</param>
 /// <param name="startPointFullName">Исходное значение свойства StartPointFullName.</param>
 /// <param name="startPointShortName">Исходное значение свойства StartPointShortName.</param>
 /// <param name="endPointGid">Исходное значение свойства EndPointGid.</param>
 /// <param name="endPointFullName">Исходное значение свойства EndPointFullName.</param>
 /// <param name="endPointShortName">Исходное значение свойства EndPointShortName.</param>
 /// <param name="cost">Исходное значение свойства Cost.</param>
 public static TripRoute CreateTripRoute(global::System.Int32 id, global::System.Int32 tripId, global::System.String startPointGid, global::System.String startPointFullName, global::System.String startPointShortName, global::System.String endPointGid, global::System.String endPointFullName, global::System.String endPointShortName, global::System.Int32 cost)
 {
     TripRoute tripRoute = new TripRoute();
     tripRoute.Id = id;
     tripRoute.TripId = tripId;
     tripRoute.StartPointGid = startPointGid;
     tripRoute.StartPointFullName = startPointFullName;
     tripRoute.StartPointShortName = startPointShortName;
     tripRoute.EndPointGid = endPointGid;
     tripRoute.EndPointFullName = endPointFullName;
     tripRoute.EndPointShortName = endPointShortName;
     tripRoute.Cost = cost;
     return tripRoute;
 }
 /// <summary>
 /// Устаревший метод для добавления новых объектов в набор EntitySet TripRoute. Взамен можно использовать метод .Add связанного свойства ObjectSet&lt;T&gt;.
 /// </summary>
 public void AddToTripRoute(TripRoute tripRoute)
 {
     base.AddObject("TripRoute", tripRoute);
 }
 public void AddRoute(TripRoute entity)
 {
     db.AddToTripRoute(entity);
 }