重定向到操作并需要传递数据 [英] Redirect to action and need to pass data

查看:21
本文介绍了重定向到操作并需要传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,可以处理针对我的问题的三个操作.

第一个是编辑操作,它返回一个带有 HTML 表单的视图,用户可以在该视图中编辑给定项目的属性.

第二个是更新操作,它接受来自浏览器的回发并更新数据库.更新成功后,我们会重定向到操作.

第三个是显示动作,显示给定项目的详细信息.此操作是我们在成功更新后重定向到的位置.

流程是:

显示 -> 编辑 -> 更新(成功:y -> 重定向到显示,n -> 返回编辑)

我想要实现的是在更新成功时触发一个标志,以便在下一个 Show 视图中我可以为用户显示一条确认消息.问题是我不是 100% 确定通过 RedirectToAction() 调用携带该数据的最佳方式.有人认为我使用的是查询字符串吗?我们已经在查询字符串中携带变量用于其他目的,但我怀疑滥用它.重定向调用如下.

RouteValueDictionary dict = Foo.GetRouteValues(bar);RedirectToAction("显示", dict);

我也读过这个问题,但如果我不需要,我对使用 TempData 属性很谨慎.

问题

感谢您的一些建议!

解决方案

抱歉,最初没有看到您关于不想使用 TempData 的说明.

简而言之 - 如果客户端刷新/重新加载他们被重定向到的页面,您是否希望您的消息重新出现?

如果这样做,则使用查询字符串,例如:

return(RedirectToAction("Index", new { message = "hi there!" }));

然后要么定义

public ActionResult Index(string message) { }

或者显式拉出 Request.QueryString["message"] 并以通常的方式通过 ViewData 将其传递给 View.这也适用于不接受来自您网站的 Cookie 的浏览器.

如果您不想再次显示该消息,则 ASP.NET MVC 1.0 提供了用于此确切目的的 TempData 集合.

TempData 属性值存储在会话状态中,直到 来自同一浏览器的下一个请求,之后它们将被清除 - 因此,如果您在返回 RedirectToAction 之前立即将某些内容放入 TempData,它将可用重定向的结果,但之后会立即清除.

这里是对 ASP.NET MVC 启动项目中 HomeController 的一个简单更改:

public ActionResult Index() {ViewData["Message"] = "欢迎使用 ASP.NET MVC!";返回视图();}[接受动词(HttpVerbs.Post)]公共 ActionResult 索引(字符串提交按钮){TempData["message"] = "您点击了" + submitButton;返回(重定向到操作(索引"));}公共 ActionResult 关于(){返回视图();}

和相应的视图/Views/Home/Index.aspx 应该包含如下内容:

<% if (TempData["message"] != null) {%><p><%=Html.Encode(TempData["message"])%></p><%}%><% using (Html.BeginForm()) {%><input type="submit" name="submitButton" value="Button One"/><input type="submit" name="submitButton" value="按钮二"/><%}%></asp:内容>

您会注意到 TempData 消息在 POST-REDIRECT-GET 序列之后立即显示,但如果您刷新页面,它将不会再次显示.

请注意,此行为在 ASP.NET MVC 2 中已更改 - 请参阅 这篇文章 了解更多信息.

I have a controller that handles three actions that are specific to my problem.

The first is the edit action which returns a view with an HTML form that the user can edit properties on the given item.

The second is the update action which accepts the post back form the browser and updates the database. When the update is successful we do a redirect to action.

The third is the show action which shows the details of the given item. This action is where we get redirected to after a successful update.

The flow is:

Show -> Edit -> Update (Sucess: y -> redirect to Show, n -> return Edit)

What I want to achieve is to have a flag tripped when the update was successful so that on the next Show view I can display a confirmation message for the user. The problem is that I'm not 100% sure on the best way to carry that data over the RedirectToAction() call. One thought I had was using a query string? We are already carrying variables around with the query string for another purpose but part of my is skeptical to abuse that. The call to the redirect is below.

RouteValueDictionary dict = Foo.GetRouteValues(bar);

RedirectToAction("Show", dict);

I've read this question as well but am leary about using the TempData property if I don't have to.

Question

Thanks for some suggestions!

解决方案

EDIT: Sorry, didn't originally see your note about not wanting to use TempData.

In a nutshell - do you want your message to reappear if the client refreshes/reloads the page they've been redirected to?

If you do, then use the querystring, something like:

return(RedirectToAction("Index", new { message = "hi there!" }));

and then either define

public ActionResult Index(string message) { }

or explicitly pull out Request.QueryString["message"] and pass it to the View via ViewData in the usual way. This will also work on browsers that aren't accepting cookies from your site.

If you DON'T want the message to display again, then ASP.NET MVC 1.0 provides the TempData collection for this exact purpose.

TempData property values are stored in session state until the next request from the same browser, after which they are cleared - so if you put something in TempData immediately before returning RedirectToAction, it'll be available on the result of the redirect but will be cleared immediately afterwards.

Here's a simple change to the HomeController in the ASP.NET MVC startup project:

public ActionResult Index() {
    ViewData["Message"] = "Welcome to ASP.NET MVC!";
    return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string submitButton) {
    TempData["message"] = "You clicked " + submitButton;
return(RedirectToAction("Index"));
}

public ActionResult About() {
    return View();
}

and the corresponding view /Views/Home/Index.aspx should contain something like this:

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
  <% if (TempData["message"] != null) { %>
    <p><%= Html.Encode(TempData["message"]) %></p>
  <% } %>
  <% using (Html.BeginForm()) { %>
    <input type="submit" name="submitButton" value="Button One" />
    <input type="submit" name="submitButton" value="Button Two" />
  <% } %>
</asp:Content>

You'll notice the TempData message is displayed immediately following a POST-REDIRECT-GET sequence, but if you refresh the page, it won't be displayed again.

Note that this behaviour has changed in ASP.NET MVC 2 - see "Passing State between Action Methods" in this article for more information.

这篇关于重定向到操作并需要传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆