开发者

Return to a url from an action method in asp.net mvc

开发者 https://www.devze.com 2022-12-19 02:36 出处:网络
I\'m using asp.net mvc. I have links associated with a list of \"Document\"...when clicked the links call an action method to add the document to a list of favourites.

I'm using asp.net mvc.

I have links associated with a list of "Document"...when clicked the links call an action method to add the document to a list of favourites.

how in the action method do I return to the same page before the "add favourite" 开发者_高级运维link is clicked? the reason is I want to maintain the querystring parameters that have paging etc

eg:

MyPage

page 1 of 3

Document1 [add to favourites] (a link that calls an action method)

Document2 [add to favourites] (a link that calls an action method)

Document3 [add to favourites] (a link that calls an action method)

Document4 [add to favourites] (a link that calls an action method)

paging is maintained within the url with querystring parameters..

When they click add i want to be able to maintain the url, as it should take into account the page number it's on


Can't you just add the current page to the action parameter?

public ActionResult AddFavourite(int? page)
{
   // generate your paged into based on page parameter
   return View(whatever_your_paged_view_is);
}


One possible way is to include the QueryStrings required in each of the links in the document list. You would pass the required querystrings via ViewData to the view that displays the document list.

<% foreach(var doc in Model) { %>
    <%= ActionLink(doc.Title, "AddtoFavorites", new { Page = ViewData["PageNumber"], Query = ViewData["Query" }) %>
<% } %>

Or something of the sort.

Then in the action method where you do the work to add the document to the "favorites":

public ActionResult AddToFavorites(int documentID, int page, string query)
{
     // Do the work to add the document to favorites
     return RedirectToAction("ActionName", new { Page = page, Query = query}); // where "ActionName" is the name of the action that the user was on before they got here
}

Another way is to store the paging information in TempData, but that would complicate things specially if you expect the user to click multiple links.


If javascript is an option call javascript:history.back()


You can use Request.UrlReferrer to get the previous url. It is part of the http protocol and is sent as a http header by the browser. Keep in mind that it depends on the browser/client implementation if it is sent with the request and might not always be there.

The best option according to me is to add your parameters for paging directly to the link.


I would send the page in an extra parameter returnUrl, this pattern is also used by the .NET team themselves in the AccountController:

<%= Html.ActionLink("LINKNAME", "ACTION", new { id = "DOCID", returnUrl = Request.Url.PathAndQuery } ) %>

Now you action would look something like:

public ActionResult ACTION(int id, string returnUrl)
{
     //do some stuff
     return Redirect(returnUrl);
}
0

精彩评论

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

关注公众号