Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been looking through trying to find some way to redirect to an Index view from another controller.

public ActionResult Index()
{                
     ApplicationController viewModel = new ApplicationController();
     return RedirectToAction("Index", viewModel);
}

This is what I tried right now. Now the code I was given to has a ActionLink that links to the page I need to Redirect too.

@Html.ActionLink("Bally Applications","../Application")

Any help please?

share|improve this question
add comment

3 Answers

up vote 59 down vote accepted

Use the overloads that take the controller name too...

return RedirectToAction("Index", "MyController");

and

@Html.ActionLink("Link Name","Index", "MyController", null, null)
share|improve this answer
1  
Ok this worked. I tried this earlier must of been a typo when I did it before. –  cjohnson2136 Oct 25 '11 at 16:05
    
@cjohnson2136: don't forget to accept as answer then ;) –  musefan Oct 25 '11 at 16:11
1  
did that would of sooner but there was a timer stopping me –  cjohnson2136 Oct 25 '11 at 16:12
    
@cjohnson2136: No probs, I heard about this timer earlier, never know it before, perhaps it is new - or I don't ask enough questions ;) –  musefan Oct 25 '11 at 16:33
    
Ahh, for us MVC newbies this was extremely helpful. Just simply redirecting to another view in a different folder represented by a different controller was getting by me until I read this. –  atconway Aug 2 '12 at 18:31
add comment

try:

public ActionResult Index() {
    return RedirectToAction("actionName");
    // or
    return RedirectToAction("actionName", "controllerName");
    // or
    return RedirectToAction("actionName", "controllerName", new {/* routeValues, for example: */ id = 5 });
}

and in .cshtml view:

@Html.ActionLink("linkText","actionName")

OR:

@Html.ActionLink("linkText","actionName","controllerName")

OR:

@Html.ActionLink("linkText", "actionName", "controllerName", 
    new { /* routeValues forexample: id = 6 or leave blank or use null */ }, 
    new { /* htmlAttributes forexample: @class = "my-class" or leave blank or use null */ })

Notice using null in final expression is not recommended, and is better to use a blank new {} instead of null

share|improve this answer
2  
In regard to your notice, for what reason is it better to use new {} instead of null? –  musefan Dec 14 '11 at 8:56
add comment

You can use the following code:

return RedirectToAction("Index", "Home");

See RedirectToAction

share|improve this answer
    
I tried that and it did not work. It gave me page could not be found error –  cjohnson2136 Oct 25 '11 at 16:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.