0

I am having issues to get a ajax GET method redirect me to a url. Yesterday I had issues to passing data from my view to controller. now however it will work, but the ajax is not redirecting me to my view. When I open my browser and click on the button which call this method I see no error showing up. Even better, if I go to "Network" I can see the desired url, if I click on it, I get redirected to the correct URL. Any idea how to fix this? Do I have to do something with the Succes funcion?

Print Screen: enter image description here When manually click on the url

 function ShowTasks() {
    //Dear Stackoverflow > This works, this is for selecting a row in the table
    var $selectedRow = $(".highlight");
    if ($selectedRow.length == 1) {
        var dcColumn = 0;
        var rdColumn = 1;
        var shopdoccodeColumn = 4;

        //assigning name to the colomn value
        var id = $selectedRow[0].children[dcColumn].innerText.trim();
        var regdate = $selectedRow[0].children[rdColumn].innerText.trim();
        var shopdoccode = $selectedRow[0].children[shopdoccodeColumn].innerText.trim();

        //ajax
        if (id && regdate && shopdoccode) {
            $.ajax({
                type: 'GET',
                url: '@Url.Action("delivery", "service"@*, new { id = "id", shopdoccode = "shopdoccode", regdate = "regdate" }*@)',
                data: { id, regdate, shopdoccode },
                success: function (data) {
                    if (data.success) {
                        console.log("Succes");
                    }

                },
                error: function (data) {
                    console.log("Error");
                }
            });
        }
    }
}

serviceController:

 [Route("/service/delivery/{id}/{shopdoccode}/{regdate}")]
        public ActionResult Delivery(string id, string shopdoccode, string regdate)
        {
            string numberPlate = Session[NumberPlateSessionVar] as string;
            if (string.IsNullOrWhiteSpace(numberPlate))
            {
                return RedirectToAction("LicensePlate");
            }
            IList<ServiceOrder> getWorkReportFormData = ServiceRepository.GetWorkReportForm(id, shopdoccode, regdate);
            IList<ServiceDelivery> deliveredTasks = ServiceRepository.DeliveredTasks(id);
            ContactsModel contactsModel = new ContactsModel();
            IList<DeliveredTaskModel> delTasksModels = new List<DeliveredTaskModel>();
            string signature = string.Empty;
            if (deliveredTasks.Count > 0)
            {
                contactsModel.Name = deliveredTasks[0].ContactName;
                contactsModel.Email = deliveredTasks[0].Email;
                contactsModel.Phone = deliveredTasks[0].PhoneNumber;
                delTasksModels = deliveredTasks.Select(x => new DeliveredTaskModel()
                {
                    Description = x.Description,
                    Info = x.PartCode,
                    Qty = x.CalcQty
                }).ToList<DeliveredTaskModel>();
                signature = Convert.ToBase64String(deliveredTasks[0].Signature);
            }
            DeliveryModel model = new DeliveryModel()
            {
                DossierCode = id,
                Contacts = contactsModel,
                DeliveredTasks = delTasksModels,
                Signature = signature
            };
            if(getWorkReportFormData.Count > 0)
            {
                Console.WriteLine("Test");
            }
            return View(model);
        }

Previous question, might be a little relative. Url action parameters using Ajax

Community
  • 1
  • 1
Катерина
  • 414
  • 3
  • 9
  • 28

2 Answers2

0

It seems your URL is incorrect. Try below code and change you URL and data: parameter as below code. (as mentioned with //********)

if (id && regdate && shopdoccode) {
            $.ajax({
                type: 'GET',
                url: '@Url.Action("delivery", "service")',    //  ********               
                data: { "id": id, "shopdoccode ": shopdoccode , "regdate": regdate }, // ********
                success: function (data) {
                    if (data.success) {
                        console.log("Succes");
                    }

                },
                error: function (data) {
                    console.log("Error");
                }
            });
        }
prog1011
  • 3,425
  • 3
  • 30
  • 57
  • Thanks but it's still the same, it generates a url when I inspect on browser as in the screenshots but it is not redirecting me to service/delivery – Катерина Apr 12 '17 at 10:24
0

Solved my issue using this:

 window.location.href = ("service/delivery?id=" + id + "&toolcode=" + toolcode + "&regdate=" + regdate); 

after the Ajax method

Катерина
  • 414
  • 3
  • 9
  • 28