【问题标题】:How to show different text according to a bool value from the SQL database?如何根据 SQL 数据库中的布尔值显示不同的文本?
【发布时间】:2016-01-19 15:11:49
【问题描述】:

为什么在视图页面中,使用以下代码显示“当前未激活”的所有行?

          <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>

【问题讨论】:

  • 请显示控制器的代码填充模型,请...

标签: sql linq razor linq-to-sql


【解决方案1】:

你在这里:

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

【讨论】:

  • 非常感谢,我试过了,但对于列表中的所有项目,它仍然显示“当前未激活”,为什么?当我调试时,它显示 item.LeadEngineers Count=0 为什么它是 0?项目有ID,为什么LeadEngineers表中的计数为0?
  • 显示项目定义
  • 您在查询时是否在您的项目中包含了 LeadEngineers 属性?看看this post关于延迟执行的信息。
  • @claire 我的意思是你如何用数据填充它。代码看起来不错。数据或填充方式存在问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 2017-01-06
  • 2017-03-30
  • 1970-01-01
  • 2014-12-18
相关资源
最近更新 更多