asp.net C#MVC:我怎么活不下去的ViewState? [英] asp.net c# MVC: How do I live without ViewState?

查看:110
本文介绍了asp.net C#MVC:我怎么活不下去的ViewState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是寻找到的WebForms转换为MVC:

I am just looking into converting WebForms to MVC:

在.NET MVC,什么概念,使ViewState的东西那不是必需的?

In .net MVC, what concepts make ViewState something thats not required?

如果一个窗体回上iteself等(即回传)?如何在页面/用户控件保持状态?

If a form is posted back on iteself etc (ie a postback)? how does the page/usercontrol maintain its state?

什么名堂的人都在做,以保持某种状态,而不是诉诸会话状态?

What tricks are people doing to maintain some kind of state and not resort to session state?

当然,一个完全无状态的环境中不能存在?

Surely, a completely stateless environment cant exist?

推荐答案

但当然可以的。事实上,网络的无状态的。任何与此相反的思想是差的,其实。

But of course it can. In fact, the web is stateless. Any thoughts to the contrary are the aberration, in fact.

Web控件都走了MVC中。有没有开火的事件在服务器端。 URL和发布表单数据 - 这是由两种不同的机制所取代。正确使用这些将取代你对ViewState的需求。

Web Controls are gone in MVC. There are no events firing on the server side. This is replaced by two different mechanisms--URLs and POSTing form data. Proper use of these will replace your need for the ViewState.

在传统的ASP.NET Web应用程序,你可以放置在您的网页一个LinkBut​​ton,将执行职能十ASP.NET会坚持很多ViewState的克鲁夫特,javascript和其他的东西进入的网页,这样,当用户点击按钮和回发的网站(通过提交表单没人知道存在),ASP.NET重建发生了什么,并确定一个特定的按钮的事件处理程序必须执行。

In a conventional ASP.NET web application, you would place a LinkButton on your webpage that would perform function X. ASP.NET would stick lots of ViewState cruft, javascript and other stuff into the webpage so that, when the user clicks on the button and "posts back" to the website (by submitting a form nobody knows existed), ASP.NET reconstructs what happened and determines a particular button event handler must be executed.

在MVC中,你构建你的链接访问特定的路线。路线描述了用户希望做的事情 - /用户/拖欠/指数(显示所有欠费用户的列表)。 MVC中的路由系统确定哪些控制器会处理这条路线,并在该控制器上哪种方法将被执行。任何额外的信息可以通过URL查询字符串值被发送到控制器方法(?页= 5为罪犯的第5页)。

In MVC, you construct your link to access a particular route. The route describes what the user wishes to do--/Users/Delinquent/Index (show a list of all delinquent users). The routing system in MVC determines which Controller will handle this route and which method on that controller will execute. Any additional information can be transmitted to the controller method by URL query string values (?Page=5 for the 5th page of delinquents).

除了网址,你可以使用HTML表单发送回更复杂的信息(如窗体的价值的数据)或事物,将不适合在查询字符串,如文件。

In addition to URLs, you can use HTML forms to POST back more complex information (such as a form's worth of data) or things that won't fit in a query string, such as a file.

所以,你通过查询字符串和表单POST值维持状态。你会发现,其实,没有那么多的状态到底要维护。其实,不必维持大量的状态是一个很好的迹象,缺乏设计,或者你试图做一些事情,不适合网站的模式。

So you "maintain" state via query strings and form POST values. You'll find that, in fact, there isn't that much state to maintain in the end. In fact, having to maintain lots of state is a good indication that your design is lacking or that you're trying to do something that doesn't fit a website model.

这篇关于asp.net C#MVC:我怎么活不下去的ViewState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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