Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MVC3  redirect to url  question

Posted on 2014-07-31
11
Medium Priority
?
2,044 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
Independent Software Vendors: 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!

 
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
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Suggested Courses

772 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