0

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

6
  • Is it possible for you to make a redirect on ajax call return (for example, by using Jquery ajax success setting you can implement some code at the return of a call really easily), or do you absolutely have to redirect with C# ? Commented Dec 13, 2021 at 8:20
  • I kind of need to redirect from C# as I ned to pass a model to the page that is generated based on the list that was sent from the initial javascript list. I have started looking at the following but not much luck eg: window.location.href = '@Url.Action("Index", "Sandbox")' + list; Commented Dec 13, 2021 at 8:54
  • Could you add in the question what you have tried so far? This is to prevent comment / answers with solutions that you already tried Commented Dec 13, 2021 at 9:10
  • I have searched for: asp net core window location href with parameters asp.net mvc best results but not working as expected Commented Dec 13, 2021 at 9:20
  • window.location.href ="Sandbox/Index?test=" + 'My test string' This code sends a single string as expected. I want to send a list. So a dirty solution would be to add list items to string and coma delimit. But is that the best solution? Commented Dec 13, 2021 at 9:28

1 Answer 1

2

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 });
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Well done! Don't forget to mark your answer as the accepted answer :)
This is the most acceptable answer in the world.
Can only accept my answer in 2 days, its a bit silly but will remember to do so. Thank you guys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.