简体   繁体   中英

In MVC how to go back dynamically

In the Help Desk system that I am creating - I have a view for editing Tickets which is working completely. However, after the person saves the edits and goes back to the previous list view (which can be any one of 8 different views). So, I found code to go back to the previous view:

<a href="javascript:void(0);" onclick="history.go(-1);">Back to Ticket List View</a>

Which it does work in the sense that it goes back to the list view. Unfortunately though when it goes back any changes made are not reflected in the list view. After I hit refresh everything is correct. How do I make it so the user does not need to hit Refresh?

Here is my EditUserTicket controller code:

public ActionResult EditUserTicket(Guid id)
        {

            var model = db.Tickets
                    .Include(t => t.TicketNotes)
                    .Where(t => t.TicketId == id).First();

            return View(model);
        }

You might pass the return URL which is the URL of your Ticket List View to your Edit Ticket View in the form of ViewModel. Your EditTicketViewModel should be something like:

public class EditTicketViewModel
{
  public String ReturnUrl {get;set;}
  public Guid TicketId {get;set;}
}

And your Edit method of TicketController:

[HttpGet]
public ActionResult EditUserTicket(EditTicketViewModel model)
{
   var model = db.Tickets
              .Include(t => t.TicketNotes)
              .Where(t => t.TicketId == model.TicketId).First();
   return View(model);
}

In order to pass the return URL to the above controller, you will need to embed the view model in the hyperlink of the page that provides navigation to your EditUserTicketView. Replace the your hyperlink with this HTML helper:

@Html.ActionLink("hyperlinkText", "EditUserTicket", "yourTicketControllerName", 
              new EditTicketViewModel 
              {TicketId = yourTicketGuid, ReturnURL = Request.RawUrl}, null);

This only partially solved the problem, the EditUserTicketView now contains the return URL when GET request is sent but the return URL will be lost when the user performs POST request to save the data. I do not know whether you are using the AJAX approach in saving the data or the conventional synchronous POST request. If conventional approach is used, you will need to pass the return URL in your form when posting so that it can be used when redirecting back to the EditUserTicketView.

And your the return hyperlink of your EditUserTicketView.cshtml should be something like:

<a href="@Model.ReturnUrl">Back to Ticket List View</a>

Hope that it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM