简体   繁体   English

如何在不使用MVC回发会话的情况下保存数据?

[英]How do I save data without using sessions for postback in MVC?

I have 2 texboxes and 1 needs validation. 我有2个texboxes和1个需要验证。 I have this setup with Html.ValidationSummary display. 我有这个设置与Html.ValidationSummary显示。 I would like to keep the data that has been entered on the validation post. 我想保留验证帖子中输入的数据。 Instead the page is refreshed and the data is lost which makes the user enter the data again. 而是刷新页面并丢失数据,这使得用户再次输入数据。 How do I prevent that without using sessions? 如何在不使用会话的情况下阻止此操作?

Why don't you follow the pattern outlined in this blog post since postbacks technically don't exist as a concept in ASP.NET MVC: 为什么不遵循本博客文章中概述的模式,因为回发在技术上并不作为ASP.NET MVC中的概念存在:

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

No session? 没有会话? You need a cookie. 你需要一个cookie。

You can either: 你可以:

Save a unique identifier in the cookie and then use a database to store and retrieve the validation messages. 在cookie中保存唯一标识符,然后使用数据库存储和检索验证消息。

Store the validation messages in the cookie. 将验证消息存储在cookie中。

This should be handled out-of-box. 这应该是开箱即用的。 When you return the view on validation failure, the values in the ViewModel should be automagically overriden by the values that were originally posted. 当您在验证失败时返回视图时,ViewModel中的值应该由最初发布的值自动覆盖。 The ModelBinder stores the input names and their posted values in the ModelState. ModelBinder将输入名称及其发布值存储在ModelState中。 So, my guess is the ModelState is being misused. 所以,我的猜测是ModelState被滥用了。

If you use MVC2, you can use MVC Futures Html.Serialize & Deserializing attribute. 如果使用MVC2,则可以使用MVC Futures Html.Serialize&Deserializing属性。

Exploring the ASP.NET MVC 2 futures assemby 探索ASP.NET MVC 2期货组合

Check this site. 检查此网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM