Marius Vasile

Marius Vasile

  • 600
  • 1.7k
  • 125.3k

Razor View if condition within foreach group to show either an a link

Sep 4 2021 11:39 AM

I have a foreach and several conditions within. Basically, I want to show an a link if PTWNoS is null and a span if PTWNoS is not null. The problem is I have a group and I don't know how to build the condition, similar with the one in the second foreach

 

@foreach (var group in Model.PTWContents.GroupBy(s => new { s.PTWMain.PTWId, s.PTWMain.PTWNo }).OrderByDescending(s => s.Key.PTWNo))
                    {
                        <div class="row no-gutters">
                            @if (User.IsInRole("PTWIsoCert"))
                            {
                                <div class="col-md-1">
                                    <a class="form-control text-center btn-link" style="height:62px;" asp-page="/PTW/IsolationCertificate" asp-route-ptwid="@group.Key.PTWId">@Html.DisplayFor(modelItem => group.Key.PTWNo)</a>
                                </div>
                            }
                            else if (User.IsInRole("PTWIsoCert"))
                            {
                                <div class="col-md-1">
                                    <span class="form-control text-center" style="height:62px;">@Html.DisplayFor(modelItem => group.Key.PTWNo)</span>
                                </div>
                            }
                            else
                            {
                                <div class="col-md-1">
                                    <a class="form-control text-center btn-link" style="height:62px;" asp-page="/PTW/PTWView" asp-route-ptwid="@group.Key.PTWId">@Html.DisplayFor(modelItem => group.Key.PTWNo)</a>
                                </div>
                            }
                            <div class="col-md-11">
                                @foreach (var item in group.OrderBy(s => s.StartDate))
                                {
                                    <div class="row no-gutters">

                                        @if (item.PTWSubsequent != null && item.PTWSubsequent.PTWNoS != 0)
                                        {
                                            @if (User.IsInRole("PTWIsoCert"))
                                            {
                                                <div class="col-md-2">
                                                    <a class="form-control text-center btn-link" style="height:62px;" asp-page="/PTW/IsolationCertificate" asp-route-ptwid="@item.PTWId" asp-route-ptwsid="@item.PTWSId">@Html.DisplayFor(model => item.PTWMain.PTWNo) - @Html.DisplayFor(model => item.PTWSubsequent.PTWNoS)</a>
                                                </div>
                                            }
                                            else
                                            {
                                                <div class="col-md-2">
                                                    <a class="form-control text-center btn-link" style="height:62px;" asp-page="/PTW/PTWView" asp-route-ptwid="@item.PTWId" asp-route-ptwsid="@item.PTWSubsequent.PTWSId">@Html.DisplayFor(modelItem => item.PTWMain.PTWNo) - @Html.DisplayFor(modelItem => item.PTWSubsequent.PTWNoS)</a>
                                                </div>
                                            }
                                        }
                                        else
                                        {
                                            <div class="col-md-2">
                                                <textarea rows="2" readonly class="form-control text-center bg-white" style="resize:none">No Subsequent</textarea>
                                            </div>
                                        }

                                        <div class="col-md-2">
                                            <textarea rows="2" readonly class="form-control text-center bg-white" style="resize:none">@Html.DisplayFor(model => item.PTWType)</textarea>
                                        </div>
                                        <div class="col-md-4">
                                            <textarea rows="2" readonly class="form-control bg-white" style="resize:none">@Html.DisplayFor(model => item.WorkScope)</textarea>
                                        </div>
                                        <div class="col-md-1">
                                            <textarea rows="2" readonly class="form-control text-center bg-white" style="resize:none">@Html.DisplayFor(model => item.StartDate)</textarea>
                                        </div>
                                        <div class="col-md-1">
                                            <textarea rows="2" readonly class="form-control text-center bg-white" style="resize:none">@Html.DisplayFor(model => item.EndDate)</textarea>
                                        </div>
                                        <div class="col-md-2">
                                            <textarea rows="2" readonly class="form-control text-center bg-white" style="resize:none">@Html.DisplayFor(model => item.PTWStatus)</textarea>
                                        </div>
                                    </div>
                                }
                            </div>
                        </div>
                    }

 


Answers (2)