user3474542
user3474542

Reputation: 33

Redirecting to the View from controller

Hi I have two controllers and and two views in my application.

  1. Home Controller & Index View
  2. Susbcription Controller & Index View.

I have a save method in the Subscription Controller. I want to call the Home Controller/ Index View after the save is complete. When I do the following I get the page not found error.

return Redirect("Home/Index");

OR

return View("Home/Index");

Could somebody tell me why is the view not getting called ?

Thanks and regards Ranjit Menon

Upvotes: 0

Views: 1034

Answers (2)

Sam
Sam

Reputation: 687

To add on top of Ehsan's answer, I want to point out how to pass parameters around if you need to do this in the future:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult PaymentBillMe(string pledgeAmt, string month, string radioBtn, string btn) 
    { 
      return RedirectToAction("PaymentConfirm", new
                        {
                            paymentSelection = radioBtn,
                            paymentAmt = pledgeAmt,
                            selectedMonth = month
                        });
    }

    [HttpGet]
    public ActionResult PaymentConfirm(string paymentSelection, string paymentAmt, string selectedMonth)
    {
     //Do something with parameters...
     return View();
    }

Here I am passing the radio button selection, pledge amount, and the selected month to the payment confirm method, doing something with those parameters and then returning that view.

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

you have to use RedirectToAction as mostly your action Return type is ActionResult :

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

or if you want to return simply view without action, then:

return View("~/Views/Home/Index.cshtml");

See more details here

Note:

The second is not recommended you should use first one to redirect to Home/Index, you should not use Rediect in asp.net mvc you should use RedirectToAction

Upvotes: 3

Related Questions