How does Asp.net MVC return to the original page after a task is executed?

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.