This is my first question as I usually find the answer from another question.
I have a simple rentals program. On the details page of the rental property there are two tables, one for tenants and one for serviceable items. On the items table there is a column for Service in Progress. I need to iterate through all of the services and if there are Any null values for the completionDate on any of the services for that one item, the text should be "Yes". If the completionDate for All of the services have a value then the text should be "No". I have a foreach loop to iterate over the services right now and a break if the text should be "Yes". When the first service is completed and another is not completed, it creates a column for each answer. It should only have one column with one answer.
Relevant part of the Rentals Model
public List<Tenant> Tenants { get; set; }
public List<Item> Items { get; set; }
public List<ItemType> ItemType { get; set; }
Relevant part of the Item Model
public byte Active { get; set; } = 1;
public ItemType ItemType { get; set; }
[Display(Name = "Purchase Price")]
[DisplayFormat(DataFormatString ="{0:C}")]
public decimal PurchasePrice { get; set; }
public List<Service> Services { get; set; }
Relevant part of the Service Model
[Column(TypeName ="date")]
[Display(Name = "Completion Service Date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? CompletionServiceDate { get; set; }
Relevant part of the View
<table >
<thead>
<tr>
<th>Item Type</th>
<th>Update</th>
<th>Purchase Price</th>
<th>Service in Progress</th>
</tr>
</thead>
<tbody>
@if (Model.Items.Count > 0 || Model.Items != null)
{
foreach (var item in Model.Items)
{
if (item.Active == 1)
{
<tr>
<td>@Html.ActionLink(item.ItemType.Name, "Details", "Items", new { id = item.Id }, null)</td>
<td>
@Html.ActionLink("Move Item", "MoveItem", "Items", new { id = item.Id }, null) |
@Html.ActionLink("New Service", "New", "Services", new { id = item.Id }, null) |
@Html.ActionLink("Sell/Junk Item", "Update", "Items", new { id = item.Id }, null)
</td>
<td>[email protected]</td>
@if (item.Services.Count > 0)
{
foreach (var service in item.Services)
{
if (service.CompletionServiceDate == null)
{
<td>Yes</td>
break;
}
else
{
<td>No</td>
}
}
}
else
{
<td>No Services</td>
}
</tr>
}
}
}
</tbody>
If the first service has null value for completionDate, then it shows Yes in the table and breaks out of the loop. If the first service has a real value for the completionDate, then it shows No and the loop continues. I only want it to say No in the table if all of the services have a real value for the completionDate. I have been looking at for loops, do and do while loops to go inside my foreach loop. I need to check all of the services, but I only need one answer not an answer for each service. Any help would be greatly appreciated.
Tried another way in the View
@if (item.Services.Count > 0)
{
bool v = false;
foreach (var service in item.Services)
{
if (service.CompletionServiceDate == null)
{
<td>Yes</td>
break;
}
else
{
v = true;
}
}
if (v ==true )
{
<td>No</td>
}
}
else
{
<td>No Services</td>
}
CodePudding user response:
Column is added because you loop through the Services. You can check with Count
to find all of the respective service has a completion date or not.
@if (item.Services.Count > 0)
{
int completedService = item.Services.Where(x=>x.CompletionServiceDate !=null).Count;
if (completedService != item.Services.Count)
{
<td>Yes</td>
}
else
{
<td>No</td>
}
}
else
{
<td>No Services</td>
}
this logic will work for you like charm.