2016-01-19 22 views
0

爲什麼在視圖頁面中顯示下列代碼中的所有行「當前未激活」?如何根據SQL數據庫中的布爾值顯示不同的文本?

  <td> 
      @{ 
       if (item.LeadEngineers.All(n => n.Active == false)) 
       { 
        @: Not active currently 

       } 
       var y = item.LeadEngineers.Where(n => n.Active == true).ToList(); 
       if (y.Count() > 0) 
       { 

        @: Currently active 
       } 
      } 

     </td> 

我要檢查,如果該項目的另一個表LeadEngineerstruefalse,以顯示該項目是目前活躍與否。

該項目已在表LeadEngineers多條記錄,我要檢查所有的記錄/行具有Active=False,則顯示「未激活」,如果有一列(至少一個)具有Active=true,然後顯示「活動現在」。

item的定義是在視圖模型LeadIndexData

public class LeadIndexData 
{ 
    //to show each item's full name 
    public IEnumerable<WebUser> WebUsers { get; set; } 

    //to show each item's assigned techniques 
    public IEnumerable<Technique> Techniques { get; set; } 

    //to show each item's supervised employees 
    public IEnumerable<EmployeeTech> EmployeeTechs { get; set; } 

    //to show if it's active or not 
    public IEnumerable<LeadEngineer> Leads { get; set; } 
} 

我的控制器:

public ActionResult Index(int? id, int? techId) 
    { 
     var viewModel = new LeadIndexData(); 
     var query = (from b in db.LeadEngineers 
        join a in db.WebUsers 
        on b.User_ID equals a.User_ID 
        where a.Active == true 
        select new 
        { 
         User_ID = b.User_ID, 
         User_FirstName = a.User_FirstName, 
         User_Surname = a.User_Surname 
        }).AsEnumerable().Select(x => new WebUser 
        { 
         User_ID = (int)x.User_ID, 
         User_FirstName = x.User_FirstName, 
         User_Surname = x.User_Surname 
        }).Distinct().ToList(); 

     //put the query results into the list of leads 
     var engineers = query.AsEnumerable(); 

     //use HashSet to hold a set of lead webusers 
     HashSet<WebUser> vme = new HashSet<WebUser>(); 

     //access each item in the leads list and assign the lead webuser's details to each 
     foreach (var wu in engineers) 
     { 
      var w = new WebUser { User_ID = wu.User_ID, User_Surname = wu.User_FullName }; 
      if (!(vme.Any(x => x.User_ID == wu.User_ID))) 
      { 
       vme.Add(w); //add the lead webuser details to the hashSet 
      } 
     } 
     //put the webusers details vme in the leadengineer viewmodel 
     viewModel.WebUsers = vme.ToList(); 
     ..... 
     } 

我的索引視圖頁:

 @model Version0.Models.LeadIndexData 
    .... 
    .... 
    @foreach (var item in Model.WebUsers) 
    { 
    string selectedRow = ""; 
    if (item.User_ID == ViewBag.userId) 
    { 
     selectedRow = "success"; 
    } 
    <tr class="@selectedRow" valign="top"> 
     <td> 
      @Html.DisplayFor(modelItem => item.User_Surname) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.User_FirstName) 
     </td> 
     <td> 
      @{ 

       if (item.LeadEngineers.Any(n => n.Active == true)) 
       { 
        @: Currently active 
       } 
       else 
       { 
        @: Not active currently 
       } 
      } 

     </td> 
      .... 
     </table> 
+0

請顯示控制器填充模型的代碼,請... –

回答

0

給你:

<td> 
     @{ 
      if (item.LeadEngineers.Any(n => n.Active == true)) 
      { 
       @: Currently active 
      } 
      else 
      { 
       @: Not active currently 
      } 
     } 
    </td> 
+0

非常感謝,我試過了,但它仍然顯示列表中的所有項目都顯示爲「Not active」,爲什麼?當我調試時,它顯示item.LeadEngineers Count = 0爲什麼它是0?該項目有其ID,但爲什麼LeadEngineers表中的計數爲0? – claire

+0

顯示項目 –

+0

的定義查詢時是否在項目中包含LeadEngineers屬性?看看[這篇文章](http://stackoverflow.com/questions/33921262/is-deferred-execution-in-asp-net-mvc-view-a-very-bad-thing/33921573#33921573)延期執行。 – Shyju

相關問題