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>