Solved

MVC3  redirect to url  question

Posted on 2014-07-31
11
1,808 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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)

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now