为什么ASP.NET MVC不支持ViewState? [英] Why ASP.NET MVC does not support ViewState?

查看:80
本文介绍了为什么ASP.NET MVC不支持ViewState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为什么ASP.NET MVC不支持ViewState?任何人请用一个很好的例子来解释。

提前谢谢

Hi,
Why ASP.NET MVC does not support ViewState? Anyone please explain with a good example.
Thanks in advance

推荐答案

因为需要一种机制来存储额外的webforms控件需要ViewState数据,以便可以在回发时重新创建控件。由于回发中仅包含表单元素,因此页面上不是表单值的任何内容都将丢失。例如,asp:Label没有html表单元素,因此没有地方存储标签的文本,它是前面的颜色等等,所以数据进入ViewState,所以当你做回发时它会知道标签的文本是什么是什么颜色。



MVC不支持webforms控件,你需要自己管理所有html渲染和陈述。无论如何,ViewState只是一个隐藏的字段,如果需要,您仍然可以使用相同的技术来存储有关控件的数据。
ViewState was required for webforms controls as they needed a mechanism to store additional data so that the controls could be recreated on postback. As only form elements are included in postbacks, anything on the page that isn't a form value is lost. For example an asp:Label has no html form element so there is nowhere to store the label's text, it's fore colour and so on, so that data went in ViewState, so when you did a postback it would know what the text of the label was and what colour it was.

MVC doesn't support webforms controls, you need to manage all the html rendering and state yourself. ViewState was just a hidden field anyway, and you can still use the same technique for storing data about controls if you need to.


Quote:

ASP.NET MVC不使用传统意义上的ViewState(在网页中存储控件的值)。相反,控件的值将发布到控制器方法。一旦调用了控制器方法,你对这些值的处理取决于你。



ASP.NET MVC会将控件的值保持足够长的时间给你验证它们并(如果需要)将它们往返回到您的页面进行编辑或更正。如果控件验证,您可以将它们保存到数据库或其他数据存储中,在那里它们可用于后续的GET请求。

ASP.NET MVC does not use ViewState in the traditional sense (that of storing the values of controls in the web page). Rather, the values of the controls are posted to a controller method. Once the controller method has been called, what you do with those values is up to you.

ASP.NET MVC will persist the values of the controls long enough for you to validate them and (if needed) to round-trip them back to your page for editing or correction. If the controls validate, you can persist them to a database or other data store, where they will be available for subsequent GET requests.



和red 这个 [ ^ ]。



-KR


and red this[^] as well.

-KR


这篇关于为什么ASP.NET MVC不支持ViewState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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