ViewData和TempData [英] ViewData and TempData

查看:113
本文介绍了ViewData和TempData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有两个控制器用于登录,另外一个用于添加详细信息。

现在成功登录我正在提取电话号码并转到添加控制器。

我可以通过使用Tempdata变量但不能使用ViewData或ViewBag来实现这一点???



对于添加控制器我有添加视图与手机号码字段。与此同时,我必须填充该领域。怎么办呢。



同样还有其他的控制者,我必须传递手机号码。

Hi
I have two controllers one for login and other for Adding details.
Now on successful login I am extracting phone number and passing to to "Add" controller.
I am able to do this by using Tempdata variable but not with ViewData or ViewBag???

Second for for "Add" controler I have "Add" view with mobile number field. At the sametime I have to populate that field. How can it be done.

Similarly there are other controlers in which I have to pass the mobile number.

推荐答案

ViewBag和ViewData不会在页面请求之间保留,它们仅用于将数据从控制器传递到其视图。 TempData使用Session,并且Session会在整个用户访问时保持不变,但TempData会在您读取时删除Session中的数据,因此它是一次性变量。如果您需要保留多个用途的数据,请使用Session而不是TempData。
ViewBag and ViewData are not persisted between page requests, they are only for passing data from controller to its view. TempData uses the Session, and the Session is persisted for the entirety of the user's visit, however TempData deletes the data from the Session when you read it so it's a one-use variable. If you need the data kept around for multiple uses then use the Session rather than TempData.


这篇关于ViewData和TempData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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