Reputation: 23
I'm trying to change certain word by click-able link with the Replace function. The way the application works is when I put a label somewhere on the page I can dynamically choose which word can be a link.
My problem happen when I replace the text with the tag. Once I load the page, I don't get a link, I only get the raw text. Here's the sample code: (Excuse the none optimized code, I'm just trying to make it works)
var text = ViewBag.isEnglish ? Model.TextEnglish : Model.TextFrench; //Get the text either in french or english
int counter = 0;
foreach(var option in Model.FieldItems)
{
text = text.Replace(ViewBag.isEnglish ? option.TextEnglish : option.TextFrench, "{" + counter + "}");//Replace the selected text to {#}
counter++;
}
counter = 0;
foreach(var option in Model.FieldItems)
{
if (ViewBag.isEnglish) {
text = text.Replace("{" + counter + "}",string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", option.Value, option.TextEnglish));//Replace the {#} with the <a> tag here
}
else
{
//text = text.Replace("{" + counter + "}", "<a href=\""+option.Value+"\" target=\"_blank\">"+option.TextFrench+"</a>");
}
counter++;
}
if (ViewBag.isEnglish)
{
Model.TextEnglish = text;
}
else
{
Model.TextFrench = text;
}
<div id="field@(Model.FieldID)" class="field form-group @Model.Classes.Format("Field")" data-fieldid="@Model.FieldID">
<span for="@id" class=" @Model.Classes.Format("Label")">@(ViewBag.isEnglish ? Model.TextEnglish : Model.TextFrench)</span>//Show the text here
</div>
I am trying to not use the Microsoft helper. I don't know what I am doing wrong. Is there a way to like not get a raw text from this? Thank you for reading and answering
Upvotes: 1
Views: 324
Reputation: 1309
Instead of using just
@(ViewBag.isEnglish ? Model.TextEnglish : Model.TextFrench)
try using
@Html.Raw(ViewBag.isEnglish ? Model.TextEnglish : Model.TextFrench)
which will render out the HTML instead of just plaintext
Upvotes: 1