2015-05-20 49 views
3

我的代碼是:「Models.LoginViewModel」是一種類型,這是不是在給定的情況下有效

@sample.Models.LoginViewModel 
@{ 
    string user = Session["users"].ToString(); 
} 
@{ 
    ViewBag.Title = "Home Page"; 
} 

我得到的錯誤:

'@sample.Models.LoginViewModel' is a type, which is not valid in the given context.

請給任何想法來解決我的問題。

+0

究竟是什麼你想在這裏做什麼?該語法不清楚。指定更多細節。 –

+0

嘗試@model sample.Models.LoginViewModel – Sachu

回答

4

變化碼

@sample.Models.LoginViewModel 

@model sample.Models.LoginViewModel 

即是

@model sample.Models.LoginViewModel 

    @{ 
     if(Session["users"] != null) 
     { 
     string user = Session["users"].ToString(); 
     } 
    } 
    @{ 
     ViewBag.Title = "Home Page"; 
    } 

@model關鍵字應該是在開始ü[R只是僅指定型號名稱

+0

如果我使用@model,我解決了上述問題,但我面臨另一個問題 –

+0

'/'應用程序中的服務器錯誤。 未將對象引用設置爲對象的實例。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.NullReferenceException:未將對象引用設置爲對象的實例。 源錯誤: 第1行:@model MvcApplication127.Models.LoginViewModel 第2行:@ { 線路3:字符串用戶=會話[ 「用戶」]的ToString(); 第4行:} 第5行:@ { –

+0

檢查'@ {if(Session [「users」]!= null){string user = Session [「users」]。ToString();}'試試這個 – Sachu

4

你做錯

@sample.Models.LoginViewModel 

insted的使用

@model sample.Models.LoginViewModel 
2

你的語法是錯誤的,改變

@sample.Models.LoginViewModel@model sample.Models.LoginViewModel

相關問題