【发布时间】:2018-11-03 18:17:45
【问题描述】:
我正在尝试通过带有替换功能的可点击链接来更改某些单词。应用程序的工作方式是当我在页面的某处放置标签时,我可以动态选择哪个单词可以作为链接。
当我用标签替换文本时,我的问题发生了。加载页面后,我没有得到链接,我只得到原始文本。这是示例代码:(请原谅没有优化的代码,我只是想让它工作)
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>
我正在尝试不使用 Microsoft 帮助程序。我不知道我做错了什么。有没有办法不喜欢从中获取原始文本? 感谢您的阅读和回答
【问题讨论】:
-
我想你在输出
Model.TextEnglish|French时仍然需要使用@Html.Raw进行渲染,即使你在代码中使用了它 -
@(ViewBag.isEnglish ? Model.TextEnglish : Model.TextFrench)->@Html.Raw(ViewBag.isEnglish ? Model.TextEnglish : Model.TextFrench)应该可以工作 -
我想没有别的办法了。谢谢!
-
是的,否则它只会将其视为纯文本并按原样写入。
标签: c# html asp.net asp.net-mvc model-view-controller