如何使用 DisplayName 属性和/或 LabelFor 在表单标签中呈现 HTML 链接?
我有一个评论表单,我试图在 HTML 标签中呈现指向 Markdown 引用的 HTML 链接。我尝试将链接添加到视图模型中的 DisplayName
属性:
[DisplayName("Comment (you can format comments with <a href=\"http://daringfireball.net/projects/markdown/syntax\">Markdown</a>)")]
public string Body { get; set; }
这会导致以下显示:
我还尝试直接在视图中添加标签:
@Html.LabelFor(x => x.Comment.Body, "Comment (you can format comments with <a href=\"http://daringfireball.net/projects/markdown/syntax\">Markdown</a>)")
但结果是可以理解的相同。
我意识到这是因为 MVC 是 HTMLEncoding
的输出,以确保安全,但是有没有办法按标签关闭此功能,或者在这种情况下我只需要在视图中手动写出 HTML 标签?
I have a comment form where I am trying to render an HTML link to the Markdown reference within an HTML label. I tried adding the link to the DisplayName
attribute in my view model:
[DisplayName("Comment (you can format comments with <a href=\"http://daringfireball.net/projects/markdown/syntax\">Markdown</a>)")]
public string Body { get; set; }
Which results in the following display:
I also tried adding the label directly within the view:
@Html.LabelFor(x => x.Comment.Body, "Comment (you can format comments with <a href=\"http://daringfireball.net/projects/markdown/syntax\">Markdown</a>)")
But the result is understandably the same.
I realise this is because MVC is HTMLEncoding
the output for safety, but is there any way to turn this off per label, or do I just have to manually write out an HTML label in my view in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
时隔 8 年,我遇到了这个问题,实际上您可以使用解决方法来解决这个问题。
将模型中的代码更改为:
将其添加到您的视图中:
这是一个小提琴来查看它。
After a 8 year gap, I encountered this problem and you can in fact use a workaround to get this working.
Change the code in your model to this:
Add this into your view:
Here is a fiddle to see it.
恐怕您必须手动执行此操作。所有 HTML 帮助程序都只是对内容进行 HTML 编码。
I am afraid that you will have to do this manually. All HTML helpers simply HTML encode the content.