2014-09-13 34 views
0

我想根據簡單的條件返回視圖,但無法這樣做。看起來好像我錯過了一個非常簡單的東西在某個地方,但無法弄清楚。根據if條件返回視圖

我想要做的是根據用戶ID返回不同的意見。

如果下面的代碼是「usercontext.UserId!= 1」,它的工作原理!但它不適用於usercontext.UserId == 1.我在這裏錯過了什麼?

public ActionResult Index() 
    { 
     UserProfile usercontext = new UserProfile(); 
     if (usercontext.UserId == 1) { 
      return View(db.Employees.Where(x => x.DepartmentID == 2).ToList()); 
     } 
     else { 
     return View(db.Employees.ToList()); 
     } 
    } 
+2

因爲用戶ID始終爲0 – 2014-09-13 07:26:34

+0

如何?你能解釋我嗎? – sensahin 2014-09-13 07:27:59

+1

,因爲你在上面的行中實例化它。 – paqogomez 2014-09-13 07:30:26

回答

2

你有空的usercontext模型和UserId屬性是默認的(0爲int)。你必須找到不同的方式來查找UserId。

0

你沒有得到用戶ID Correctly.Try這段代碼

var usercontext = new UserProfile(); 
var userid = User.Identity.UserId; 
if (userid == 1) { 
    ...rest of the code