2015-03-25 37 views
0

我正在嘗試向索引操作添加條件以僅獲取特定記錄。故事是這樣的。在控制器索引操作中篩選記錄

  1. 註冊的用戶具有所有者或ServiceCompany的身份角色
  2. 有一個主表和服務公司的桌子,每個人都有一個RegUserID持有的基礎上,該角色從標識表中的用戶名用戶。
  3. 有一個資產表(它將被索引),它具有一個OwnerID和一個Service CompanyID字段。
  4. 根據登錄用戶的UserID,我需要識別用戶角色,然後使用RegUserID從相關表中獲取OwnerID或ServiceCompanyID,然後根據角色列表使用OwnerID或ServiceCompodeID的資產

我有儘可能...

// GET: Assets 
public ActionResult Index() 
{ 
    if(Roles.GetRolesForUser().Contains("Owner") == true) 
    { 
     // Get OwnerID from Owners table where Owners.RegUserID == User.Identity.GetUserId(); 
     // Get list of assets from Asstes where Assets.OwnerID == OwnerID 
    } 
    else if(Roles.GetRolesForUser().Contains("Service Company") == true) 
    { 
     // Get ServiceCompanyID from Owners table where ServiceCompanies.RegUserID == User.Identity.GetUserId(); 
     // Get list if assets from Assets where Asstes.ServiceCompanyID == ServiceCompanyID 
    } 

    return View(assets); 
} 

我完全卡在如何讓OWNERID或ServiceCompanyID那麼資產的列表返回到視圖。

回答

0

你的數據庫設置等不知道。但根據你的問題,你應該能夠到這個不用擔心什麼「角色」,用戶與LINQ查詢類似於以下

var userId = User.Identity.GetUserId();  
var asset = from a in context.Assets 
    join o in context.Owners on a.OwnerId equals o.OwnerId into oa 
    from asst in oa.DefaultIfEmpty() 
    join s in context.ServiceCompanies on a.ServiceCompanyId equals s.ServiceCompanyId into sa 
    from service in sa.DefaultIfEmpty() 
    where asst.RegUserId == userId || service.RegUserId == userId 
    select a; 

return View(asset); 
+0

謝謝,我今天晚些時候會看看並報告。 – Spionred 2015-03-25 21:04:07

相關問題