asp.net mvc - Render string to html in MVC -
a similar question asked here answer did not solve problem below.
how can render string html? basically, need pass string view has "a" tag definition in it. want render actual html link 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="http://www.google.com">http://www.google.com</a>" return view(model); }
in view (at first line):
@model mynamespace.viewmodel.myviewmodel
then below, these html markups (1st lines after numbers) displays results (2nd lines). none of them actual links can click on.
1 @model.mylink <a href="http://www.google.com">http://www.google.com</a> 2 <pre>@html.raw(@model.mylink)</pre> <a href="http://www.google.com">http://www.google.com</a> 3 @html.encode(@html.raw(@model.mylink)) &lt;a href=&quot;http://www.google.com&quot;&gt;http://www.google.com&lt;/a&gt; 4 @html.encode(@model.mylink) result same #3.
any appreciated. in advance.
in controller use
model.mylink = httputility.htmldecode(url);
then in view use
@html.raw(model.mylink)
the first converts use