Many times, after deleting an element, we all want to return to the original page. How can we achieve this?
Assume that the index. cshtml (or aspx) of xcontroller contains:
<a>
Delete </a>. To return to the index page after deletion, you can use:
<a>
In xcontroller. CS
[HttpPost]ActionResult Delete(int id){ try { ... return RedirectToAction(...); } catch { }}Changed:
[HttpPost]ActionResult Delete(int id, string backUrl){ try { ... return Redirect(backUrl); } catch { }}Request. a URL contains all the address information (including parameters), which is a URI type rather than a string. Therefore, you can perform many additional operations (such as address/Server/query parameters ), you can search for details online.
I have never tried "/area/controller/Action/ID". It should be roughly the same.
Supplement: when there is "&" in the backurl (multiple parameters are to be returned), an error occurs when writing directly., Required:
<a>To encode the Returned Code (convert all special characters into % or something, and run the same in Chinese ).
When I write more, I find it troublesome to make a helper:
public static partial class Helper { public static string ReturnUrlOf(this HtmlHelper htmlHelper, HttpRequestBase request) { return HttpUtility.UrlEncode(request.Url.ToString()); } }You can write HTML. returnurlof (request) where you want to write those things (request is an attribute of view, and it is also called on demand as HTML ).
However, If You encapsulate <A> like yourself, you can encapsulate the interface as follows:
@Html.Link("x", "/SFC/Categories/Delete?rootID=" + root.ID + "&id=" + Model.ID, returnRequest: Request)All the encode and request problems are handled internally in the Link. This is the ultimate practice.
Click to download the free agile development textbook: Martian agile development manual