2

I want to redirect from a ViewResult, so that I can display a specific product detail when the search result is 1 instead of the product list page.

Other pages said to change the ViewResult to an ActionResult and to use the RedirectToAction. But my problem is that my controller inherits from a third party control and my search method is set to override the same third party control method, which is a ViewResult.

I am wondering if anyone has some suggestions on what I could do, if there is another way to redirect from a ViewResult or if there is another way to handle the override of the third party controls search method to be an ActionResult.

tereško
  • 58,060
  • 25
  • 98
  • 150
user1408767
  • 677
  • 1
  • 8
  • 30
  • You need to post what you have, but ActionResult is compatible with both ViewResult and RedirectToRouteResult. see this question http://stackoverflow.com/questions/2538469/how-do-i-redirect-within-a-viewresult-or-actionresult-function – Brian Aug 10 '12 at 15:30

2 Answers2

2

You could just return a different View (without the redirect)

//in your action method
if( result.Count == 1 )
    return View( "Product", result[0] );

or call the other action method directly (if they are in the same controller)

if( result.Count == 1 )
    return YourActionDetails( result[0].Id /* params */ );

or, you could return a simple View that makes an automatic redirect client-side

// a "Redirect" view
@model Product
<html>
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     <meta http-equiv="Refresh" content="0;url=@Url.Action("Details", Model.Id)" />
    </head>
    <body></body>
</html>
Alex
  • 7,728
  • 3
  • 35
  • 62
  • Thanks for the response. Unfortunately, the detail page returns an action result which is also set as override. I may look into that last option. – user1408767 Aug 10 '12 at 18:40
0

If you cannot change the return type of the action than you could use routing to route requests with 1 to a different action.

Just define another route and make sure that it's added to RouteCollection before the default one.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126