简体   繁体   English

在MVC中如何动态返回

[英]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. 在我正在创建的Help Desk系统中-我有一个用于编辑票证的视图,该视图可以正常运行。 However, after the person saves the edits and goes back to the previous list view (which can be any one of 8 different views). 但是,此人保存所做的编辑并返回到上一个列表视图之后(可以是8个不同视图中的任何一个)。 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: 这是我的EditUserTicket控制器代码:

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. 您可以以ViewModel的形式将返回URL(即故障单列表视图的URL)传递给“编辑故障单视图”。 Your EditTicketViewModel should be something like: 您的EditTicketViewModel应该类似于:

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

And your Edit method of TicketController: 以及TicketController的Edit方法:

[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. 为了将返回URL传递给上述控制器,您需要将视图模型嵌入提供可导航到EditUserTicketView的页面的超链接中。 Replace the your hyperlink with this HTML helper: 使用此HTML帮助器替换您的超链接:

@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. 这仅部分解决了问题,EditUserTicketView现在包含发送GET请求时的返回URL,但是当用户执行POST请求以保存数据时,返回URL将丢失。 I do not know whether you are using the AJAX approach in saving the data or the conventional synchronous POST request. 我不知道您是使用AJAX方法来保存数据还是常规的同步POST请求。 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. 如果使用常规方法,则在发布时需要在表单中传递返回URL,以便在重定向回EditUserTicketView时可以使用它。

And your the return hyperlink of your EditUserTicketView.cshtml should be something like: 您的EditUserTicketView.cshtml的返回超链接应类似于:

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

Hope that it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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