ASP.NET MVC:如何创建一个基本的伪视图状态?或者更好的解决方案? [英] ASP.NET MVC: How to create a basic Pseudo Viewstate? Or better solution?

查看:114
本文介绍了ASP.NET MVC:如何创建一个基本的伪视图状态?或者更好的解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要新望MVC和了解所有关于它的伟大的事情,包括原因视图状态不可用,但也有一些情况下,我认为有某种形式的视图状态将是非常方便,在我的情况我正在考虑的各种搜索筛选器列表页中,可以应用到列表中。

I am newish to MVC and understand all the great things about it, including the reasons why viewstate isn't available, however there are some circumstances where I think having some kind of view state will be quite handy, In my case I am thinking about list pages with various search filters that can be applied to the list.

难道是值得的实施某种形式的虚拟视图状态保持在某些情况下,这种信息?还是有更好的解决办法?

Would it be worthwhile implementing some kind of pseudo viewstate to hold this info in some cases? or is there a better solution?

在那里任何的例子吗?

您的意见AP preciated。

Your comments appreciated.

推荐答案

的理论答案

视图状态是ASP.NET的WebForms的认为这是非常有害的概念之一。它是用来存放一些控件的状态,并呈现一个非常,非常丑陋的隐藏字段到HTML。
这是不可取的一些人对SEO(搜​​索引擎优化)等方面的原因。

ViewState is one of the concepts of ASP.NET WebForms which is considered VERY harmful. It is used to store the state of some controls and renders a very-VERY ugly hidden field into the HTML.
This is undesirable for some people for SEO (search engine optimization) and other reasons.

MVC并没有提供一些的WebForms确实,对于一些很基本的原因,抽象的:

MVC doesn't provide some of the abstractions that WebForms does, for some very basic reasons:

  • 它可以让你完全控制你的网址,并输出HTML
  • 的WebForms控制(尤其是先进的)呈现垃圾HTML,MVC让你写你自己的HTML
  • 在为了避免这些故障,MVC不使用的控制,事件的原则,这样的东西
  • 如果你想你的web框架抽象掉真正的出路HTTP的行为,则应该继续使用的WebForms

事实上,HTTP是一个无国籍协议,该协议的WebForms试图从你通过引入控制的概念来掩饰,他们的状态和事件。
MVC不骗你,不会试图隐瞒你什么。

In truth, HTTP is a stateless protocol, which WebForms try to hide from you by introducing the concept of Controls, and their state and events.
MVC doesn't lie to you and doesn't try to hide anything from you.

的实际anwser

您可以使用的ViewData 而不是的ViewState
您可以设置在控制器的项目(计算机[东西] = yourObject ,然后你可以检索在查看。你可以用它来记住任何你想要的。

You can use ViewData instead of ViewState.
You set an item (ViewData["Something"] = yourObject) in the Controller, and then you can retrieve it in the View. You can use it to "remember" whatever you want it to.

因此​​,基本上,坚持信息包括从的Request.QueryString 的Request.Form 在阅读它相应的控制器的行动,将其设置成的ViewData ,然后检索的ViewData 查看信息

So, basically, persisting the information consists of reading it from Request.QueryString or Request.Form in the appropriate Controller action, setting it into the ViewData, and then retrieving the ViewData information in the View.

例如:

控制器操作:

string textBoxValue = Request.Form["myTextBox"];
...
ViewData["myTextBox"] = textBoxValue;

查看:

<% using (Html.BeginForm()) { %>
<%= Html.TextBox("myTextBox") %>
<% } %>

更多的东西

看了一些MVC相关的问题在这里(如<一个href="http://stackoverflow.com/questions/2788045/why-mvc-instead-of-good-old-asp-net-still-not-grasping-why-i-should-go-this-rou/2788180#2788180">this 之一),并宣读了MVC书(至少是免费的NerdDinner章)。
MVC将更加理解你的话,我保证!

Read some MVC-related questions here (such as this one), and read the MVC book (at least the free NerdDinner chapter).
MVC will be much more understandable for you then, I promise!

这篇关于ASP.NET MVC:如何创建一个基本的伪视图状态?或者更好的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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