asp.net mvc的对象保持活着,信息 [英] asp.net mvc keep object alive, information

查看:94
本文介绍了asp.net mvc的对象保持活着,信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code

  [HttpPost]
公众的ActionResult指数(LoginModel loginModel)
{
    如果(ModelState.IsValid)
    {
       // code的一些行。唧唧歪歪
       TempData的[loginModel] = loginModel;
       返回RedirectToAction(索引,premium);
     }
     ...
}

和该控制器在这里

 公众的ActionResult指数()
{
   VAR loginModel = TempData的[loginModel]作为LoginModel;
   ...
}

现在,页面加载时,一切似乎都正常工作。但是当我刷新,一切都弄乱了,它说,loginModel就像空。问题是,我能怎么样跟踪当前登录的用户。我已启用窗体身份验证。 TNX

误差如下


 对象引用未设置到对象的实例。说明:执行当前Web请求的执行过程中发生未处理的异常。请查看有关错误的详细信息的堆栈跟踪以及它起源于code。异常详细信息:System.NullReferenceException:对象不设置到对象的实例。源错误:
第22行:
第23行:VAR loginModel = TempData的[loginModel]作为LoginModel;
第24行:用户名字符串= loginModel.username;
第25行:字符串密码= loginModel.password;
第26行:premiumModel.username =用户名;


解决方案

困惑


  

但是当我刷新,一切都弄乱了,它说,loginModel
  就像空


这是由于这一事实表明您已经阅读了的TempData 键,读出一次后,数据就会丢失该特定密钥。

  VAR值= TempData的[是keyName] //读取一次,数据都将丢失



  我

怎么样跟踪当前登录用户的


所以,坚持数据读取数据可以活着像下面

即使经过

  VAR值= TempData的[是keyName];
TempData.Keep(); //数据也不会丢失所有密钥
TempData.Keep(是keyName); //数据也不会丢失此键

的TempData 工作在新的标签/ Windows还,如会话变量一样。

您可以使用会话变量也只有主要问题是,会话变量都非常繁忙,<$比较C $ C>的TempData 。最后,你能够保持跨控制器/地区也是数据。

希望这篇文章可以帮助你很多。

i have this code

[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
    if (ModelState.IsValid)
    { 
       // some lines of code . bla bla bla
       TempData["loginModel"] = loginModel;
       return RedirectToAction("index", "premium");
     }
     ...
}

and this controller here

public ActionResult Index()
{
   var loginModel = TempData["loginModel"] as LoginModel;
   ...
}

now, when the page loads, everything seems to work fine. but when i refresh, everything messes up, it says that the loginModel is like null. the question is, how can i like keep track of the current login users. i have forms authentication enabled. tnx

error is as below


Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web     request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 22: 
Line 23:             var loginModel = TempData["loginModel"] as LoginModel;
Line 24:             string username = loginModel.username;
Line 25:             string password = loginModel.password;
Line 26:             premiumModel.username = username;

解决方案

Confusion

but when i refresh, everything messes up, it says that the loginModel is like null

Answer

This is due to the fact that you have read the TempData key and Once it is read, data will be lost for that particular key.

var Value = TempData["keyName"] //Once read, data will be lost


Question

how can i like keep track of the current login users

Answer

So to persist the data even after the data is read you can Alive it like below

var Value = TempData["keyName"];
TempData.Keep();                 //Data will not be lost for all Keys
TempData.Keep("keyName");        //Data will not be lost for this Key

TempData works in new Tabs/Windows also, like Session variable does.

You could use Session Variable also, Only major problem is that Session Variable are very heavy comparing with TempData. Finally you are able to keep the data across Controllers/Area also.

Hope this post will help you alot.

这篇关于asp.net mvc的对象保持活着,信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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