Home > front end >  ASP.NET MVC: why is this code within my view displaying "<br>" rather than a line br
ASP.NET MVC: why is this code within my view displaying "<br>" rather than a line br

Time:07-08

I have some code in my view which displays an employee's info (name, address, etc), within a grid. I'm having a problem however, when an employee's "Address 2" has data. In this circumstance, I need to place the Address 2 on a new line, within the same grid cell as Address 1.

So it should display as follows:

Name Address
John Smith 123 My St
Amy Andrews 456 Main St
Apt B.

Instead however, my Address 2 is displaying the following, which physically displays the "br" tag:

456 Main St<br>Apt B.

Here's my code, which basically says, display "AddressLine1" and IF "AddressLine2" is not null, then include a line break, followed by AddressLine2.

I know I can get this to work by breaking out the AddressLine1 & AddressLine2 segments & I'm not against it, but I'd prefer to keep it similar to the code that already exists, which is written similar to the following, if possible:

@if (member.Employee != null)
 {
      <td>
            @(member.Employee.FullName != null ? member.Employee.FullName : "-" )
      </td>
      <td>
            @(member.Employee.AddressLine1 != null ? member.Employee.AddressLine1  
                                    (member.Employee.AddressLine2 != null ? "<br/>"    
                                    member.Employee.AddressLine2 : "")
                                    : "-" )
      </td>
      ...
}

Thanks for any help.

CodePudding user response:

The code doesn't emit the tag <br/> but a string that contains "<br/>". Use an @if block to emit the data you want instead, the same way you did for member.Employee :

<td>
</td>
     @(member.Employee.FullName ?? "-" )
<td>
    @(member.Employee.AddressLine1 ?? "-")
    @if(member.Employee.AddressLine2 != null)
    {
       <br/>
       @(member.Employee.AddressLine2)
    }
</td>
  • Related