MVC 在视图之间传输数据 [英] MVC Transfer Data Between Views
问题描述
我刚刚开始学习 MVC,并正在尝试了解它是如何工作的.
I just started to learn MVC and am trying to understand how it works.
我不想将用户发送到所有编辑、插入和列表操作的不同视图.
I don't want to send users to different views for all edit, insert and list operations.
在我的示例应用程序中,一个视图包含一个项目列表,在列表下方有一个表单(用于插入新项目),其操作为{Controller}/Create",但没有创建视图.
In my sample application a View contains a list of items and below the list there is a form (for inserting new items) with action "{Controller}/Create" but there is no Create View.
当用户插入一个新项目时,它使用 httpverb post 发布到 Create 操作并创建项目并使用 RedirectToAction 方法返回到 List 操作.
When a user inserts a new item it posts to the Create action with httpverb post and creates the item and returns back to the List action with RedirectToAction method.
但是我无法以这种样式向用户显示任何消息(错误、信息等),因为我无法在创建操作和列表操作之间传递数据.我该怎么做?
But I can not show any message(error, information etc) to the user in this style because I can not pass data between Create action and List action. How can I do that?
推荐答案
大多数 MVC 框架都能够通过下一个请求临时存储一小部分数据,仅用于此目的.在 ASP.NET MVC 中它称为 TempData,在 Rails 中它称为 :flash 等.
Most MVC frameworks have the ability to temporarily store a small bit of data just through the next request, for just this purpose. In ASP.NET MVC its called TempData, in Rails it's called :flash, etc.
这篇关于MVC 在视图之间传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!