2014-09-22 66 views
0

我面臨的問題,在淨MVC4處理應用程序會話數值來讀取會話值。我試圖創建和會話指定用戶ID,並試圖從另一個控制(這是在不同的地區)閱讀。但是當我讀取Session值時,它返回空值。下面是代碼[登錄頁面]無法在.net MVC 4

[HttpPost] 
[ActionName("school")] 
public ActionResult school_post(string Code, string Password) 
{ 
    if (ModelState.IsValid) 
    { 
     Validation Val = new Validation(); 
     if (Val.LoginAthenticated(Code, Password, 1)) 
     { 
      Session["schoolcode"] = Code; 
      Response.Redirect("school/dashboard"); 
     } 
     else 
     { 
      Response.Write("<script>alert('Incorrect credentials')</script> "); 
     } 

    } 
    return View(); 
} 

和儀表板代碼:

public class dashboardController : Controller 
{ 
    // 
    // GET: /school/dashboard/ 

    public ActionResult Index() 
    { 
     string test = Session["schoolcode"] as string; 
     Dashboard dashboard = new Dashboard(); 
     return View(dashboard); 
    } 

} 

我需要在上面的代碼或任何應用程序配置改變什麼?

回答

0

是你的會話[「學校代碼」] 有一個整數值。然後它只會返回null,而你使用Session [「schoolcode」]作爲字符串進行轉換。

嘗試會話[ 「schoolcode」]。的ToString()。你會得到你的價值

+0

它包含字符串。我已經嘗試了.ToString()和Convert.ToString()但沒用。 – prince 2014-09-22 09:52:49

+0

你在web.config中烏爾<配置> <的sessionState模式= 「INPROC」 超時= 「20」/> DineshKumar 2014-09-22 10:05:15

+0

我解決了問題,有這樣的代碼。我取代了Response.Redirect(「學校/儀表板」);帶返回RedirectToAction( 「索引」, 「儀表板」,新的{面積= 「學校」});謝謝@DineshKumar – prince 2014-09-22 10:08:58