locked
Passing html from controller to view RRS feed

  • Question

  • User1396448631 posted

    I am passing the following ViewBag from controller to view

    ViewBag.Links = "<a href='www.google.com' target='_blank'>Google</a>. <a href='www.microsoft.com' target='_blank'>Microsoft</a>";

    In view I have the following

    <p>@Html.Raw(ViewBag.Links)</p>

    When this view is rendered, it displays Google and Microsoft. However, the url it points to is relative, i.e.

    http://localhost/LinkWebApp/Home/www.google.com

    which opens a tab with 

    HTTP Error 404.0 - Not Found

    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

    What should I do so that the link opens?

    Monday, February 11, 2019 7:49 PM

Answers

  • User475983607 posted

    You're missing the https.  The browser thinks the link is relative to the site.

    ViewBag.Links = "<a href='https://www.google.com' target='_blank'>Google</a>. <a href='https://www.microsoft.com' target='_blank'>Microsoft</a>";

    I would pass the a list of URLs and have the View wrap the URLs in HTML as that's what View and HTML helpers are designed to do.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 11, 2019 10:08 PM