Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MVC3  redirect to url  question

Posted on 2014-07-31
11
1,848 Views
Last Modified: 2014-08-02
I want to call a action in a controller (currently ajax call is there), and once it reaches the action, I need to redirect it to some  other url say www.yahoo.com

so I don't want to return anything to the view but redirect, trying the following:

  public ActionResult ManageActivation(ManageActivationParameters inputs)
        {
                Redirect(string.Format(script));
      }

Open in new window


script is what I am dynamically creating and contains the logic so that url opens in new tab.

how should I call this ManageActivation action from view and what type of modifications I need to make there.
0
Comment
Question by:Dinesh Kumar
  • 6
  • 4
11 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40232219
If you are doing an AJAX call to the server, the server does not have the power to redirect you.   It COULD return a value to the client which you might evaluate in JavaScript to do a redirect on the client side using Javascript.

You CAN do a redirect if you are doing a full postback.
0
 

Author Comment

by:Dinesh Kumar
ID: 40232269
how to call the mvc action without using AJAX then and how to redirect to third party url?
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40232365
If you are doing something in a context in which a new view would be called up (say in the About action of your Home controller, you can simply do
public ActionResult About()
        {
            return Redirect("http://www.google.com");
        }

Open in new window

to redirect to google.

If you are going to continue using an Ajax call, you want to insert javascript redirect logic in a callback on the client side (in the view) using whatever information your controller sends you.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 15

Expert Comment

by:Insoftservice
ID: 40232535
Response.Redirect("http://www.microsoft.com");
0
 

Author Comment

by:Dinesh Kumar
ID: 40233635
I can open  page in new tab by using following in javascript:
javascript:void window.open ('www.yahoo.com?id=encrypted data', 'window name')

but in case of mvc3, I need to send some data to action which will have some encryption logic on data and then I want to send that data in new page  in new tab.  I don't want to give any data back to the current page that will be same as it was but new tab to be opened.

The above are not working.
0
 

Author Comment

by:Dinesh Kumar
ID: 40233637
I am not having any control over the new tab page url, only needs to send data to that and opening that in new window.
0
 

Author Comment

by:Dinesh Kumar
ID: 40233981
Hi Jens,

its working:

public ActionResult About()
        {
            return Redirect("http://www.google.com");
        }


but only thing is to open it in new tab.

I am calling the About() using    location.href
0
 

Author Comment

by:Dinesh Kumar
ID: 40234129
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 40235061
If I were doing something like this, I'd probably use an AJAX action and do the work in Javascript.

With the following as an action in the Region controller:
        [HttpPost]
        public ActionResult GetContent(string x)
        {
            return Content("http://www.google.com");
        }

Open in new window


I would use this in the view:
@using (Html.BeginForm())
{
    <button id="ajaxsubmit" type="button">Query</button>
}

<script type="text/javascript">
    $('#ajaxsubmit').click(function () {
        $.ajax({
            url: "Region/GetContent",
            type: "POST",
            data: {x: "data-to-pass"},

            success: function (result) {
                window.open(result, '_blank')
            }
        });
        return false;
    });
</script>

Open in new window


(note that since this uses jQuery you will want to load jquery at the TOP of the layout, not at the bottom)
0
 

Author Closing Comment

by:Dinesh Kumar
ID: 40236182
Thank you.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40236297
My pleasure
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where is this file? 3 25
Input parameteres to DragOver 2 19
Ahax pagination 9 32
How to get total count of passed in subject based on marks? 7 16
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question