简体   繁体   English

如何在Mvc帮助器中将Html.Raw(x)作为字符串参数传递?

[英]How to pass an Html.Raw(x) as string parameter in a Mvc helper?

I am not sure that even the question is correct, so I am trying to explain what I want to achieve and hope you can help me with the best solution. 我不确定这个问题是否正确,因此我试图解释我想要实现的目标,并希望您能为我提供最佳解决方案。

I am using in many places in my project the bootstrap modal, so I made an helper to make my life easier. 我在项目的许多地方都使用了引导程序模版,因此我提供了一个帮助程序,使我的生活更轻松。

The helper is working with only one exception, I am not able to give the body content in the correct format. 助手仅工作一个例外,我无法以正确的格式提供正文内容。

The helper need 4 parameters to run: 助手需要4个参数才能运行:

@Html.MyModal(string bodycontent, string title, string closeBtnText, string id)

I have the bodycontent stored in the database like html, and I was planing to use this helper @Html.Raw(bodycontent) like this: 我将bodycontent像html一样存储在数据库中,并且我@Html.Raw(bodycontent)像这样使用此帮助程序@Html.Raw(bodycontent)

 @Html.MyModal(Html.Raw(model.Content).toString(), model.Title, resources.Close, model.Id)

Obviously is not working, still showing the content with html tags. 显然无法正常工作,仍然显示带有html标签的内容。

Here is the helper code which handle the bodyContent: 以下是处理bodyContent的帮助程序代码:

var bodyTag = new TagBuilder("div");
    bodyTag.MergeAttribute("class", "modal-body");
    bodyTag.MergeAttribute("style", "overflow-y: scroll;height:250px;");
    bodyTag.SetInnerText(bodycontent);

bodycontent is helper parameter, of type string. bodycontent是辅助参数,类型为字符串。

Can you please help me with a solution in order to display the bodycontent like plain text (in database is stored like html)? 您能帮我一个解决方案,以便以纯文本形式显示正文内容(在数据库中以html格式存储)吗?

One solution to my problem, is: 解决我的问题的一种方法是:

  1. I changed the data type for bodycontent from string to IHtmlString 我将bodycontent的数据类型从string更改为IHtmlString
  2. I replace the following line in the helper: 我在帮助器中替换以下行:

    bodyTag.SetInnerText(bodycontent); bodyTag.SetInnerText(bodycontent);

with: 与:

 bodyTag.InnerHtml=bodycontent.ToHtmlString();

and now my helper is working as expected: 现在我的助手正在按预期方式工作:

 @Html.MyModal(Html.Raw(model.Content), model.Title, resources.Close, model.Id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM