简体   繁体   English

在Razor中,如果URL已存在于模型的属性中,该如何在图像中显示?

[英]In Razor, how do I show in image if the URL is already in a property of my model?

I have a class which has a property called PictureURL . 我有一个类,它具有一个称为PictureURL的属性。 It contains an absolute URL to the picture which should represent it as a string. 它包含图片的绝对URL,该图片应将其表示为字符串。

public class MyClass
{
    public string PictureURL {get;set;} 
    public string Name {get; set;}
} 

I want to display it in a partial view. 我想以局部视图显示它。

@model MyClass
<div> 
    <img src="@Model.PictureUrl" />
    @Model.Name
</div> 

The problem is that MVC encodes the property for the display. 问题在于MVC对显示的属性进行编码。 So my URL which contained an ampersand gets mangled. 因此,我的包含“&”号的网址被弄乱了。 An example gravatar identicon would become http://www.gravatar.com/avatar/0d958098841b2060bf685088b33e6662?d=identicon&amp;s=256 . 实例identicon的示例将变为http://www.gravatar.com/avatar/0d958098841b2060bf685088b33e6662?d=identicon&amp;s=256 Of course, the browser renders nothing with this broken img tag. 当然,浏览器使用此损坏的img标记不呈现任何内容。

As far as I know, there is no HTML helper which builds <img> tags. 据我所知,没有建立<img>标签的HTML帮助器。 And when I search for displaying images in Razor, the results I get are about properly resolving relative URLs. 当我搜索Razor中的显示图像时,得到的结果是关于正确解析相对URL的。

What would be a good way to build the img tag properly? 正确构建img标签的一种好方法是什么? (I own the code of MyClass and can change its property type from string to something else, if this would help). (如果有帮助,我拥有MyClass的代码,可以将其属性类型从字符串更改为其他类型)。

@Html.Raw(Model.PictureUrl)应该解决此问题。

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

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