Home > Net >  Syntax Help for If, Else on Razor Index Page
Syntax Help for If, Else on Razor Index Page

Time:01-06

I have a table on an index page in my razor page app. If the Applicant Last Name is null (or field is empty in database), I would like to have the Applicant Company Name be placed in the cell instead.

I'm just not getting the syntax correct though and could use a hand, please. Here is what I currently have. Note: The comma between @obj.ApplicantLHame and @obj.ApplicantFName seems to be an issue as well. I would like this to look like: Doe, John

Results currently do not return any Applicant Company Names and @obj.ApplicantLName @obj.ApplicantFName returns DoeJohn (w/o the space)

<table id="ReferralTable"  style="width:100%">
        <thead>
            <tr>
                <th>
                    <a asp-page="./Index" asp-route-sortOrder="@Model.RefNoCompleteSort">Referral No</a> / Tax Map No
                </th>
                <th>
                    Municipality
                </th>
                <th>
                    Referring Board
                </th>
                <th>
                    Applicant
                </th>
                <th>
                    Application Type - Class
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach(var obj in Model.Referral)
            {
            <tr>
                <td width="15%">@obj.RefNoComplete</td>
                <td width="30%">@obj.RefMunicipality</td>
                <td width="20%">@obj.RefAgencyName</td>
                <td width="20%">
                @if(@obj.ApplicantLName is null)
                    {
                        @obj.ApplicantCompany
                    }
                    else
                    {
                        @obj.ApplicantLName, @obj.ApplicantFName
                    }
                </td>
                <td width="15%">@obj.ApplicationType - @obj.CurrentClass</td>
            </tr>
            <tr>
                <td colspan="1">@obj.TaxMapNo</td>
                <td colspan="4">@obj.Comments</td>
            </tr>
            }
        </tbody>
    </table>

Thanks for any and all assistance you can provide!

CodePudding user response:

Your code in order to work you will have to make it look like below:

@if (obj.ApplicantLName is null)
{
    @obj.ApplicantCompany
}
else
{
    <text>@obj.ApplicantLName, @obj.ApplicantFName</text>
}

or you can replace it with the following one line code.

@(obj.ApplicantLName is null ? obj.ApplicantCompany : obj.ApplicantLName   ", "   obj.ApplicantFName)
  • Related