重定向到行动,需要传递的数据 [英] Redirect to action and need to pass data

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

问题描述

我有一个控制器来处理特定于我的问题三个动作。

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

首先是它会返回一个HTML表单视图,用户可以编辑给定项属性的编辑操作。

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.

的流程是:

显示 - >编辑 - >更新(Sucess:Y - >重定向到显示,N - >返回编辑)

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

我想实现的是有一个标志跳闸时更新成功,这样在下次显示视图,我可以为用户显示一个确认信息。问题是,我不是100%肯定上进行过RedirectToAction()调用数据的最佳方式。一想到我是使用查询字符串?我们已经背着变量与周围的查询字符串用于其他目的,但我是持怀疑态度滥用的那部分。到重定向的呼叫如下。

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);

我读过这个问题很好,但我猜疑有关使用TempData的财产,如果我没有。

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

<一个href=\"http://stackoverflow.com/questions/1936/how-to-redirecttoaction-in-asp-net-mvc-without-losing-request-data\">Question

感谢您的一些建议!

推荐答案

编辑:对不起,没有原来看到不想使用TempData的备忘

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!" }));

,然后要么定义

public ActionResult Index(string message) { }

或明确拔出的Request.QueryString [消息],并把它传递给通过的ViewData以通常的方式查看。这也将在未从您的网站接受cookie浏览器。

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.

如果您不希望显示的消息再次,然后ASP.NET MVC 1.0提供了TempData集合为此确切的目的。

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的属性值存储在会话状态,直到接下来从同一个浏览器,之后,他们将被清除的请求 - 所以,如果你把东西的TempData立即返回RedirectToAction之前,这将是可用在重定向的结果,但会被随即被清除。

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.

下面是一个简单的改变HomeController的在ASP.NET MVC启动项目:

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();
}

和相应的视图/Views/Home/Index.aspx应该包含这样的:

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>

您会注意到TempData的消息在POST-REDIRECT-GET序列立即显示以下,但如果刷新页面,就不会再次出现。

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.

请注意,这种行为在ASP.NET MVC 2已经改变 - 看到的这篇文章以获取更多信息。

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天全站免登陆