如何重新使用的模型数据上后回来MVC [英] How to re-use model data on post back with MVC

查看:109
本文介绍了如何重新使用的模型数据上后回来MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递结构化数据到我的MVC页面加载时开始。用户提交一个接触的形式,我想重新使用相同的数据(我只是离开同一页上),所以我不必再访问数据库。我声明的变量全局控制器存储模型数据,但它是在后回末尾的空,所以看起来我不能有重复使用它。

I'm passing structured data to my MVC page when it loads initially. After the user submits a contact form, I want to re-use the same data (I just "leave" the same page up) so I don't have to hit the database again. I declared a variable global to the controller to store the model data, but it's null at the end of the post back, so it looks like I can't re-use it there.

好像这将是一个典型的场景。我该如何处理呢?

Seems like this would be a typical scenario. How do I handle it?

推荐答案

如果你想要重用视图模型或其他检索到的数据是不会要回发的一部分,您可以

If you are wanting to reuse viewmodel or other retrieved data that is not going to be part of the postback, you can either

一)输出它隐藏字段,以便它回发到你的动作(MEH)或

a) Output it in hidden fields so that it is posted back to your action (meh) or

B)存储在会话对象(S),因此,这将是提供给您的应用程序的任何其他控制器/动作。如果你担心的内存,你可以删除会话变量你重用后,如果你不将需要再次使用它。

b) Store the object(s) in Session so that it will be available to any other controllers/actions in your application. If you are worried about memory, you could delete that session variable after you reuse it if you are not going to need to use it again.

在您的初始页面加载,检查会话变量是否存在,如果是这样,你是好 - 其他填充它。

On your initial page load, check if the session variable exists, if it does, you are good - else populate it.

Oh和为什么全局变量的事情是不工作 - >一个控制器new'd为每个请求(使用缺省控制器工厂假设),并在控制器这样任何全局变量将在每个请求复位。

Oh and why the global variable thing isn't working -> a controller is new'd up for each request (assuming using the default controller factory) and as such any global variables in the controller will be reset on each request.

这篇关于如何重新使用的模型数据上后回来MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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