如何在MVC 5中使用RedirectToAction将对象传递给新的Action [英] How can I use RedirectToAction to pass an object to a new Action in MVC 5

查看:118
本文介绍了如何在MVC 5中使用RedirectToAction将对象传递给新的Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况.

用户填写了一个表单(ForexPayment),然后他/她单击下一步.

A form (ForexPayment) is filled by a user, he/she then clicks Next.

然后我仅显示标签和值的新页面(ForexPaymentConfirmation)(即只读确认页面),这样他/她就可以提交取消请求.

I then show new page (ForexPaymentConfirmation) with just the labels and values (i.e. a read-only confirmation page) so he/she can either Submit or Cancel the request.

我的问题:

  • 如何使用 return RedirectToAction("ForexPaymentConfirmation"); 将包含付款信息"的对象从一个视图传递到另一个视图?

  • How do i use return RedirectToAction("ForexPaymentConfirmation"); to pass an object containing the Payment Information from one view to the next?

一种选择是将付款信息存储在当前会话中,并在新页面上将其收回...

One option would be to store the Payment Information in the Current Session and reclaim it on the new page ...

是否有更好的方法来实现这一目标?

Is there a better way of achieving this?

通过SO搜索,我发现: https://stackoverflow.com/a/7599952/44080 其中指出:

Searching through SO, I found this: https://stackoverflow.com/a/7599952/44080 which states:

您实际上是在尝试使用控制器进行数据访问.

You are actually trying to use controllers to do data access.

但是在这种情况下,我尚未保存付款,我需要在其他视图上预览,然后保存.

But in this case, I have not saved the payment yet, I need to preview on a different View, before saving.

使用WebForms,我只需取消隐藏同一页面上的面板即可实现这一目标.

推荐答案

可以使用

return RedirectToAction("ForexPaymentConfirmation", model);

但是,内部会创建一个丑陋的查询字符串,其中包括属性的所有名称和值

However, internally this creates an ugly query string including all the names and values of your properties

../ForexPaymentConfirmation?somePropertyName=someValue&anotherPropertyName=AnotherValue.....

仅当您的模型包含值类型的属性( int bool DateTime 等)或 string .如果任何属性是复杂的对象或集合,则绑定将失败.此外,您可以轻松地超出查询字符串的限制,并抛出异常.

It will only work if your model contains properties which are value types (int, bool DateTime etc) or string. If any properties are complex objects or collections, binding will fail. In addition you could easily exceed the query string limit and throw and exception.

目前尚不清楚为什么需要这种模式(与保存模型相反,以防用户改变主意,然后在详细信息"视图中显示删除"按钮),但是您需要将模型保留在某个地方.您可以使用 Session (但您不应该使用 TempData ),但是将其持久保存到某种形式的存储库(例如数据库)(可能是另一个临时"表)或将其序列化为xml文件,以便您可以在GET方法中对其进行检索.另一种选择是将其存储在永久表中,但包含一个位标志(例如 bool IsPending ),您可以在确认"时设置该标志,在取消"的情况下,只需删除表格行.

Its unclear why you need this pattern (as opposed to saving the model and then in the 'details' view having a 'delete' button' in case the user changes their mind), but you need to persist the model somewhere. You can use Session (but you should not use TempData), but its always better to persist it to some form of repository, for example to a database (could be another 'temporary' table) or serialize it to an xml file so that you can retrieve it in the GET method. Another option would be to store it in the permanent table but include a bit flag (say bool IsPending) that you could set when you 'Confirm', and in the case of 'Cancel', just delete the table row.

这篇关于如何在MVC 5中使用RedirectToAction将对象传递给新的Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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