Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting from cshtml page

I want to redirect to a different view depending on the result of a dataset, but I keep getting returned to the page I am currently on, and can't work out why. I drop into the if statement the action gets called but once i return the view to the new page, it returns me back to the current page.

CSHTML page

@{ ViewBag.Title = "Search Results"; EnumerableRowCollection<DataRow> custs = ViewBag.Customers;  bool anyRows = custs.Any(); if(anyRows == false) {       Html.Action("NoResults","Home");   } // redirect to no search results view 

}

Controller

 public ActionResult NoResults()     {        return View("NoResults");      } 

View I cant get too..

@{ ViewBag.Title = "NoResults";  }  <h2>NoResults</h2> 
like image 988
Jed I Avatar asked Jul 15 '13 11:07

Jed I


People also ask

How do I redirect on razor page?

You can use the IActionResult to return a redirection or your razor page.

How we can redirect to another page or controller?

Redirect() method The first method od redirecting from one URL to another is Redirect(). The Rediect() method is available to your controller from the ControllerBase class. It accepts a target URL where you would like to go.


1 Answers

Change to this:

@{ Response.Redirect("~/HOME/NoResults");} 
like image 200
Mostafa Soghandi Avatar answered Sep 28 '22 03:09

Mostafa Soghandi