-2

i want to redirect from view1 to view2, but it is not working and i cannot figure out why. it stays on the same view (path is still: "Home/view1" and i want it to be "Home/view2)". please help.

in my 1. view:

Html.Action("View2", "Home", new { id = siteid });

in my home-controller:

public ActionResult View2(int id) 
{

   var model = new View2ID();
   model.site1ID = id;
   return View(model);
}
  • `@Html.Action()` renders the view returned by the `View2` method in the existing view. It does not do a redirect. For that you need to generate a link - `@Html.ActionLink("View2", "View2", "Home", new { id = siteid }, null )` –  Jan 07 '18 at 11:26
  • @StephenMuecke you know how i can automatically call the link? because the "html.action(..)" is in a if-condition and if the if-condition is true i want to redirect it to view2. now with the html-actionlink, view1 gets build with that link and when i click on it, it redirects me to the desired view. but i want that i dont even have to click on that link. hope its clear what i mean with that – frogfrog Jan 07 '18 at 11:34
  • That makes no sense. If you have logic that should redirect to a different page, then that code goes in the controller, and you redirect in the controller method to the appropriate view. –  Jan 07 '18 at 22:38

1 Answers1

0

If you need to do redirect from the view you want to use

@{ Response.Redirect(Url.Action("View2", "Home", new { id = siteid }); }

Anyway, I believe it is better to do the redirect from controller action before view is going to be rendered. So, far the condition is probably known at that time.

public ActionResult View1(int id) 
{
   ...logic you already have there

   if(condition) {
       RedirectToAction("View2", "Home", new { id = siteId });
   }

   return View(model);
}

Related links:

Redirect from a view to another view

dropoutcoder
  • 2,627
  • 2
  • 14
  • 32