Link to home
Start Free TrialLog in
Avatar of Stacie
StacieFlag for United States of America

asked on

Trying to convert a Json method into an ActionResult control method

My goal is to move a (JsonResult StoreSchedulerMapViewData) method into an (public ActionResult SchedulerMapView) function that is in my controller but I'm unsure how to modify my .ajax call.

Any input is greatly appreciated: My main goal is to create a GUID token and not use my memory cache..

Here is the before code and what I tried to attempt.... Unfortunately I'm trying to modify my .ajax to call an IAction result and I can't seem to figure this out...

All I'm trying to do is not use the MemoryCache.Default.Add(guidToken, schedulerMapPrms, DateTime.Now.AddMinutes(1)); on my JsonResult StoreSchedulerMapViewData and just use the main functionality to my second controller

 public JsonResult StoreSchedulerMapViewData(string route, DateTime scheduleDate, string[] serviceIds)
    {
        var guidToken = LoggedInUser.ID.Trim() + System.Guid.NewGuid().ToString().Trim();
        var serviceIdsList = new List<int>();

        for (int i = 0; i < serviceIds.Count(); i++)
            serviceIdsList.Add(Convert.ToInt32(serviceIds.ElementAt(i)));

        var schedulerMapPrms = new SchedulerMapParams()
        {
            Route = route,
            ScheduledDate = scheduleDate,
            ServiceIds = serviceIdsList
        };
        var result = new Models.ResponseObject() { Success = true, Message = guidToken, Model = serviceIds };
        MemoryCache.Default.Add(guidToken, schedulerMapPrms, DateTime.Now.AddMinutes(1));
        return Json(result);
    }

Open in new window


public ActionResult SchedulerMapView

public ActionResult SchedulerMapView(string guidToken)
    {

        ViewBag.IsOpenLayers = true;
        var schedulerMapPrms = MemoryCache.Default.Get(guidToken) as SchedulerMapParams;

        if (schedulerMapPrms == null)
            return RedirectToAction("Index", "Scheduler", new { area = "SchedulerDevExtreme" });

        var stopsForServicesErrorResult = ServiceAssistantService.Routing.PrintRouteService.GetStopsForServiceIds(LoggedInUser.CultureInfo, schedulerMapPrms.ServiceIds);
        if (!stopsForServicesErrorResult.Success)
            return RedirectToAction("Index", "Scheduler", new { area = "SchedulerDevExtreme" });

        var calculatedRouteRequestResult = RoutingMethods.GetCalculatedRouteRequest(stopsForServicesErrorResult.Result, false);
        if (!calculatedRouteRequestResult.Success)
            return RedirectToAction("Index", "Scheduler", new { area = "SchedulerDevExtreme" });

        var routeType = RouteTypeService.GetRoute(schedulerMapPrms.Route);

        return PartialView("SchedulerMapView", new SchedulerMapViewModel(LoggedInUser.CultureInfo, 
        routeType, schedulerMapPrms.ScheduledDate, serviceCodeHtmlColors, 
        stopsForServicesErrorResult.Result, routingViewModel, haloLegendViewModel));
       }

Open in new window


View

     var schedulerMapViewURL = Url.Action("SchedulerMapView", "Routing", new { area = "Routing", 
     guidToken = "CBNR" });
     var schedulerMapViewStoreDataURL = Url.Action("Test", "Routing", new { area = "Routing" });

     razor code....
     var showMapView = function (targetDay, targetRoute) {
    let serviceIds = getServiceIdsForDayAndRoute(targetDay, targetRoute);
    if (serviceIds.length > 0) {
        var data = {
            "serviceIds": serviceIds,
            "scheduleDate": formatDateTime(targetDay),
            "route": targetRoute
            };

            $.ajax({
                type: "POST",
                dataType: "json",
                url: '@schedulerMapViewStoreDataURL',
                data: data,
                success: function (response) {
                    if (response.Success) {
                        let showMapUrl = '@schedulerMapViewURL'.replace("CBNR", response.Message);
                        window.open(showMapUrl);
                    }
                    else {
                        Loading.end();
                        GenerateStatus = false;
                        swal({
                            title: '@Error, ViewBag.LoggedInUser.CultureInfo)',
                            text: response.Message,
                            type: 'error',
                            closeOnCofirm: true,
                            closeOnCancel: true
                        });
                    }
                }
                , error: function (jqXHR, error, errorThrown) {
                    GenerateStatus = false;
                    Loading.end();
                    swal({
                        title: '@Error, ViewBag.LoggedInUser.CultureInfo)',
                        text: error,
                        type: 'error',
                        closeOnCofirm: true,
                        closeOnCancel: true
                    });
                }
            });
    } else {
        showAlert('');
    }
}

Open in new window


My attempt on my controller

 public ActionResult SchedulerMapView(string route, DateTime scheduleDate, string[] serviceIds, string guidToken)
    {

        if (guidToken == null){
        var guidTokenTest = LoggedInUser.ID.Trim() + System.Guid.NewGuid().ToString().Trim();
        var serviceIdsListTest = new List<int>();
        for (int i = 0; i < serviceIds.Count(); i++)
            serviceIdsListTest.Add(Convert.ToInt32(serviceIds.ElementAt(i)));


        var SchedulerMapParams = new SchedulerMapParams()
        {
            Route = route,
            ScheduledDate = scheduleDate,
            ServiceIds = serviceIdsListTest
        };
        var results = new RealGreenSystems.Models.ResponseObject() { Success = true, Message = guidToken, Model = serviceIds };

         MemoryCache.Default.Add(guidToken, SchedulerMapParams, DateTime.Now.AddMinutes(1));
         ViewBag.IsOpenLayers = true;
         var schedulerMapPrms = MemoryCache.Default.Get(guidToken) as SchedulerMapParams;

            if (schedulerMapPrms == null)
               return RedirectToAction("Index", "Scheduler", new { area = "SchedulerDevExtreme" });

        } else {

            ViewBag.IsOpenLayers = true;
            var schedulerMapPrms = MemoryCache.Default.Get(guidToken) as SchedulerMapParams;

            if (schedulerMapPrms == null)
                return RedirectToAction("Index", "Scheduler", new { area = "SchedulerDevExtreme" });

            var stopsForServicesErrorResult = 
         Routing.PrintRouteService.GetStopsForServiceIds(LoggedInUser.CultureInfo, 
         schedulerMapPrms.ServiceIds);

        }

        return (null) ;
    }

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.