viewbag 和 viewstate 的区别? [英] Difference between viewbag and viewstate?

查看:9
本文介绍了viewbag 和 viewstate 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Webforms 中的 ViewState 和 MVC 中的 ViewBag 在功能上有区别吗?他们似乎是同一件事".并且可以以相同的方式使用.我问是因为 MVC 促进了无状态 Web,而不是在页面中填充数据,从而导致膨胀和性能下降.但似乎您也可以在 MVC 中做到这一点.他们所做的一切,似乎只是给它一个新名字.

Is there a functional difference between ViewState in Webforms and ViewBag in MVC? They seem to be the "same thing". And can be used in the same ways. I ask because MVC promotes the stateless Web and not stuffing data in the page causing bloat and slower performance. But it seems that you can do that in MVC as well. All they did, seemingly, is just give it a new name.

推荐答案

ViewState 在 Web Forms 中将表单数据序列化为表单中隐藏的加密字段,因此数据可以重新绑定到回传.

ViewState in Web Forms was serializing form data into a hidden, encrypted field in the form, so data could be re-bound on the postback.

ViewBag/ViewData 是一个字典,您可以在其中填充"数据.例如,您可以在您的控制器中添加它,然后在您的视图中访问它.数据是动态,这使得处理数据变得困难.ViewBag 不会发送到客户端,它是 MVC(服务器管道)的一部分.

ViewBag/ViewData is a dictionary where you can "stuff" data into. For example, you might add to it in your Controller, then access it in your View. The data is dynamic which makes it difficult to work with the data. ViewBag doesn't get sent to the client, it's part of the MVC (server pipeline).

两者都应该避免.

ViewState,好吧,不使用它并寻找解决方法.而 ViewBag 应该通过使用 ViewModels 来避免.

ViewState by, well, not using it and finding workarounds. And ViewBag should be avoided by the use of ViewModels.

这篇关于viewbag 和 viewstate 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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