Martin
Martin

Reputation: 71

ASP .Net Core Call Controller Function from Javascript with redirect from Controller

I am breaking my head on this and cant find a solution. In short I have a ASP .Net Core MVC page that has a Javascript list. I need to pass that list to a redirect. I have this in place and it is working so not the solution I am looking for as it is not secure.

So what I am trying to do is calling the Controller function from Javascript passing the list. I can only do this with Ajax but the controller wont redirect on an Ajax call. Is there anyone that has encountered a similar problem and possibly a solution for this?

Javascript var -> Controller -> redirect.

Thank you in advance guys

Upvotes: 0

Views: 1759

Answers (1)

Martin
Martin

Reputation: 71

I found the solution. So leaving the answer here for any future guys having the same problem.

Javascript

    $("#btn").click(function () {
       var PostData = ["1","2","3","4","5","6","7","8","9"]
       if (PostData != "") {            
          window.location.href ="Controller/Function?test=" + PostData
                   
       } else
         alert('Oops.!!');});

C# Controller

 public IActionResult Test(string test)
    {
        return RedirectToAction("function", "Controller", new { test });
    }

Upvotes: 2

Related Questions