ViewBag / ViewData的生命周期 [英] ViewBag/ViewData Lifecycle

查看:322
本文介绍了ViewBag / ViewData的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到有关何时使用ViewBag /的ViewData VS视图模型很多帖子,但我一直没能找到ViewBag的生命周期进行了说明。

I have seen many posts about when to use ViewBag/ViewData vs ViewModel but i have not been able to find an explanation of the lifecycle of the ViewBag.

例如,我有一个控制器有两个动作方法:

For example, i have two Action methods in one Controller:

// POST: /MyModel/Edit/5
[HttpPost]
public ActionResult Edit(MyModel _mymodel){}

// GET: /MyModel/Edit/5
public ActionResult Edit(int id){}

如果我把一些值在ViewBag在GET操作方法,设置一些形式的标签,然后当他们用户点击提交按钮,方式是通过HTTP POST回发到服务器时,ViewBag值不再是POST操作方法中。

If i put some values in the ViewBag in the GET action method, to set up some Form labels, then when they user clicks 'Submit' button and the Form is posted back to the server via HTTP POST, the ViewBag values are no longer within the POST action method.

能否有人请解释(或提供参考的好文章)的ViewBag / ViewData的生命周期?

Can someone please explain (or provide reference to good article) the lifecycle of the ViewBag/ViewData ?

推荐答案

你把ViewBag / ViewData的数据是在其中填入你在它请求的生命周期中可用。 MVC没有回发。如果你需要的东西坚持了多单的要求,您应该使用会话。

The data you put in the ViewBag/ViewData is only available during the life-cycle of the request within which you populated it. MVC does not have post backs. If you need something to persist over more than a single request, you should use Session.

下面是关于ViewData的,ViewBag和TempData的之间的差异像样的文章:<一href=\"http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications\">http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

Here is a decent article about the differences between ViewData, ViewBag, and TempData: http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

这篇关于ViewBag / ViewData的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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