【发布时间】:2011-09-29 05:28:21
【问题描述】:
我有这个代码
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if (ModelState.IsValid)
{
// some lines of code . bla bla bla
TempData["loginModel"] = loginModel;
return RedirectToAction("index", "premium");
}
...
}
还有这个控制器
public ActionResult Index()
{
var loginModel = TempData["loginModel"] as LoginModel;
...
}
现在,当页面加载时,一切似乎都正常。但是当我刷新时,一切都搞砸了,它说 loginModel 就像 null。问题是,我怎样才能跟踪当前的登录用户。我启用了表单身份验证。 tnx
错误如下
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;
【问题讨论】:
-
另外,当我们刷新页面时,它不会转到 [HTTPPOST],因为它不是帖子类型。去普通控制器,普通控制器怎么会知道它是同一个访问者,知道用户名和密码。它启用了 cookie。有什么想法吗?
-
顺便说一下,TempData 存储它们的值直到下次访问,所以你只能使用一次值,这就是它为什么这样命名的原因 =)
-
@Alexander 知道如何使用 controller.user 属性吗? tnx
-
只需在控制器的某处使用
this.User
标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4