我在使用MVC視圖中的會話變量時遇到困難。在我下面的例子中,當我通過控制器將IsLoggedIn會話變量設置爲true時,我的視圖中的If條件未滿足。注:我修剪了一些不相關的代碼以便於閱讀。提前致謝!在MVC Views中使用會話變量的直接答案?
控制器:
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>
}