开发者

MVC Action History

I have two actions that list out items. Index() lists out all items and Filtered(string foo) filters the list of items based on foo. When a user creates a new item, I want to be able to redirect them back to either Index() or Filtered(string foo) based on where they were be开发者_StackOverflow社区fore.

How can I do this, or rework my actions to make this work?


You could modify your ActionResult to accept a string that contains the URL path the user comes from.

Something like this:

[HttpPost]
    public ActionResult CreateFoo(Blah model, string returnUrl)
    {
        // Do something here
        if (!String.IsNullOrEmpty(returnUrl)) // As long as a return URL was passed
        {
            return Redirect(returnUrl); // Return them to this URL
        }
        else
        {
            return RedirectToAction("Index", "Home"); // Otherwise Go Home
        }
   }

I don't have a ton of experience with ASP.NET MVC so there could be a better built in way to handle this. Googling for 'asp.net mvc redirect to requestor' might yield something more useful, that's essentially what you're wanting is to redirect back to the requesting route.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜