[英]If image = null, do nothing
I got some code getting data from a database. 我有一些代码从数据库获取数据。 Sometimes, there is also an image among that data which i fetch like this:
有时,我会像这样在数据中获取一个图像:
<img src="@Model.Pictue.Url">
The problem is that an image does not always exist in the DB which generates a null-exception. 问题在于,在生成null异常的数据库中并不总是存在映像。 Can I maybe create an if-statement checking if there is an image and if its not, step-over the image-part?
我是否可以创建if语句检查是否有图像,如果没有,则跳过图像部分?
Or is there a better way to solve it? 还是有更好的解决方法?
do like this: 这样做:
@if(Model.Picture != null)
{
<img src="@Model.Pictue.Url">
}
Try this: 尝试这个:
@if(Model.Picture != null)
{
<img src="@Model.Pictue.Url">
}
try this..You can put dummy picture if you dont have 试试看..如果没有的话,可以放虚拟图片
@if(Model.Picture.Url == null)
{
<img src="/DummyPicture">
}
else
{
<img src="@Model.Pictue.Url">
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.