我在使用 MVC 视图中的会话变量时遇到了困难。在下面的示例中,当我通过控制器将 IsLoggedIn 会话变量设置为 true 时,不满足视图中的 If 条件。注意:为了便于阅读,我剪掉了一些不相关的代码。提前致谢!
控制器:
public ActionResult Index(int id = 0)
{
JobSummaryModelHelper jobDetails = new JobSummaryModelHelper();
jobDetails.Id = id;
jobDetails.jdata = .....
return View(jobDetails);
}
public ActionResult Authenticate()
{
.....
int usercount = db.JobBoardUsers.Where(u => u.userid.Equals(un) && u.passcode.Equals(pc)).Select(u => new AccountModel() { uid = u.id }).Count();
if (usercount > 0)
{
HttpContext.Session["IsSignedIn"].Equals(true);
}
return RedirectToAction("Index", jobDetails);
看法:
.....
@if (Convert.ToBoolean(Session["IsSignedIn"]))
{
<fieldset>
<legend></legend>
<p style="color: red; font-weight: bold;">Logged in!
</p>
</fieldset>
}