如何根据SQL数据库中的bool值显示不同的文本?

时间:2016-01-19 15:11:50

标签: sql linq razor linq-to-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>

我想检查另一个表LeadEngineers中的项目值是true还是false,以显示该项目当前是否有效。

该项在表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>

1 个答案:

答案 0 :(得分:0)

你在这里:

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