niki b niki b - 10 months ago 66
HTML Question

Render string to html in MVC

A similar question was already asked here but the answer did not solve my problem below.

How can I render a string to html? Basically, I need to pass a string to the view that has an "a" tag definition in it. What I want is to render it as an actual html link that is clickable.

View Model (simplified):

public class MyViewModel
public string MyLink { get; set; }

In Controller (simplified):

public IActionResult Index()
MyViewModel model = new MyViewModel();

model.MyLink = "<a href=""></a>"

return View(model);

In View (at first line):

@model MyNamespace.ViewModel.MyViewModel

Then below, these html markups (1st lines after the numbers) displays the results (2nd lines). But none of them are actual links that you can click on.

<a href=""></a>

<a href=""></a>

&amp;lt;a href=&amp;quot;;quot;&amp;gt;;lt;/a&amp;gt;

result same as #3.

Any help will be appreciated. Thanks in advance.


In the controller use

model.MyLink = HttpUtility.HtmlDecode(url);

then in the view use


The first converts it to use