0

Why in the view page, it shows all the rows "Not active currently" with this following code?

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

I want to check if the item's value in another table LeadEngineers is true or false, to show the item is currently active or not.

The item has many records in the table LeadEngineers, I want to check if all the records/rows have Active=False, then show "not active", if there's one row (at least one) has Active=true, then show "active now".

The definition of the item is in the view Model 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; }
 }

My Controller:

    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();
        .....
      }

My Index View page:

     @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
  • Please show the code of the Controller populating the model, please... Commented Jan 19, 2016 at 16:43

1 Answer 1

0

Here you are:

  <td>
        @{
            if (item.LeadEngineers.Any(n => n.Active == true))
            {
                @: Currently active
            }
            else
            {
                @: Not active currently
            }
        }
    </td>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot, I tried with that, but it still shows "Not active currently" for all the items in the list, why? when I debug, it shows item.LeadEngineers Count=0 why it's 0? The item has its ID, but why its count in LeadEngineers table is 0?
Show definition of item
Are you including the LeadEngineers property in your item when querying ? Take a look at this post about deferred execution.
@claire i mean how you fill it with data. Code looks fine. Problem either in data or it the way how you fill it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.