Solved

MVC3  redirect to url  question

Posted on 2014-07-31
11
1,870 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
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…

679 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