2014-10-08 41 views
0

我很難從我的數據庫中獲取任何信息。 我覺得我在正確的軌道上,但總是出現此錯誤:從數據庫中登錄的用戶信息列表

Object reference not set to an instance of an object. 

在我的控制器類我有這個方法:

 [HttpPost] 
    public ActionResult Index(Oblig1.Models.User user) 
    { 
     if (Bruker_i_DB(user) != null) 
     { 
      Session["User"] = User.ToString(); 
      ViewBag.LoggedIn = true; 
      return View(); 
     } 
     else 
     { 
      Session["User"] = null; 
      ViewBag.LoggedIn = false; 
      return View(); 
     } 

    } 

,並在.cshtml文件I已經得到了這個:

@model Oblig1.Models.User 
@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
</head> 
<body> 
    <div> 
     <title>LoggedInSite</title> 
     <p>@Model.Email</p> 
    </div> 
</body> 
</html> 

這對任何人都有意義嗎? 幾天我開始得到相當沮喪的後...

+0

對我來說沒有意義,但你可能想'返回查看(用戶);' – 2014-10-08 09:53:53

+0

你不是通過模型對象來從行動中查看 – 2014-10-08 09:55:50

+0

請完全添加您的視圖.... – 2014-10-08 10:28:15

回答

0

你永遠傳遞視圖的模型,因此該行:

<p>@Model.Email</p> 

總是失敗,一個空引用異常

+0

我已添加用戶作爲回報查看(用戶)如果這就是你的意思?仍然是相同的錯誤 – 2014-10-08 10:42:49

+0

你是否在你返回視圖的兩個地方都這麼做? – 2014-10-08 10:44:55

+0

另外,你有沒有驗證方法參數'Oblig1.Models.User user'不是null? – 2014-10-08 10:45:38

相關問題