2015-11-20 28 views
-2

在MVC中,我想驗證用戶,他們在登錄時不能看到其他用戶數據。用戶只能夠CRUD自己的數據。我創建了兩個模型:在MVC中顯示當前用戶數據

public class User 
{ 
    public int id { get; set; } 
    public string Username { get; set; } 
    public string Firstname { get; set; } 
    public string Lastname { get; set; } 

    public List<Product> Products { get; set; } 
} 


public class Product 
{ 
    public int productId { get; set; } 
    public string productName { get; set; } 
    public double productPrice { get; set; } 

    public User Users { get; set; } 
} 

而且我有產品控制器,在那裏我想從CRUD其他用戶數據認證用戶。

+1

此網站用於回答具體問題,理想情況下,一旦你給它一個鏡頭。谷歌asp mvc教程,如果你不知道從哪裏開始。你應該找到一些信息。 –

回答

1

您可以讓您的代碼僅對可用於登錄用戶的產品執行CRUD操作。 根據您的要求使用以下方案。

public class User 
{ 
    public int id { get; set; } 
    public string Username { get; set; } 
    public string Firstname { get; set; } 
    public string Lastname { get; set; } 

    public List<Product> Products { 
     get{ 
      //get all products from database/storage for this user. for ex: I have called a method to get products 
      //you can filter products related to this user by sending this user id to database or using code like below 
      //Writing this code/logic in controller would be preferable 

      return GetAllProducts().Where(p => p.Users.id == this.id).ToList(); 
     } 
     set; 
    } 
} 
0

同意拉傑什,我會建議你使用當前用戶的數據和過濾拉傑什您呈現。當前用戶的數據可以包括當前登錄用戶的ID。如果您使用cookie來保留用戶的會話,則可以從控制器或過濾器屬性中的cookie中檢索他的Id:HttpContext.Current.Request.Cookies["auth_cookie"]