locked
Help figuring out 5 levels of @ indirection on Razor page RRS feed

  • Question

  • User1434241939 posted

    My code reads a website field from a database.  The field's values are things like www.google.com, www.bing.com etc without the preceding http:// (or https://).

    The MVC model returns and displays this field correctly, but I can't get it to prepend http:// and then make the entire thing clickable.

    This is the code I have:

    @foreach (var item in Model) {
    <tr>
       <td>
       @if (item.PrimarySiteName != null)
       {
          @Html.Raw(@("http://" + @item.PrimarySite))
       }
       </td>

    </tr>

    }

    Without the @Html.Raw, the field actually displays correctly, but this code doesn't even run.  Maybe I need to use <a href=""> to wrap the prepended string.  Depending upon how I mess with the code, sometimes the double slash is interpreted as a comment ( when I use @{ } for example ).  How can I get this link to display and be clickable?

    Tuesday, May 5, 2020 2:26 PM

Answers

  • User475983607 posted

    You forgot the anchor (<a>) tag.

    <a href="http://@item.PrimarySite">Link</a>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 5, 2020 4:07 PM