我有这个代码
[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;