图像绑定mvc actionresult剃刀视图 [英] Image binding mvc actionresult razor view
本文介绍了图像绑定mvc actionresult剃刀视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我绑定img标签如下
Hi all I am binding the img tag as follows
<img alt="" src='@Url.Action("UserImage", "Home")' class="avatar img-circle img-thumbnail" />
这是我从数据库中获取图像的代码
This is my code to get the image from the database
[HttpGet]
public ActionResult UserImage()
{
string user = Session["UserName"] as string;
byte[] photo = null;
var v = db.table.Where(p => p.USER_NAME == user).Select(img => img.PHOTO).FirstOrDefault();
if (v != null)
{
photo = v;
return File(photo, "image/jpeg");
}
else
return null;
}
当我在数据库中时显示图像但是当它为空时它显示如下,而不是我需要用空绑定src标签
http: //i.stack.imgur.com/rFPU0.png [ ^ ]
我尝试过:
This is displaying image when I have in the database but when it is null it is displaying as follows, instead of that i need to bind src tag with empty
http://i.stack.imgur.com/rFPU0.png[^]
What I have tried:
[HttpGet]
public ActionResult UserImage()
{
string user = Session["UserName"] as string;
byte[] photo = null;
var v = db.table.Where(p => p.USER_NAME == user).Select(img => img.PHOTO).FirstOrDefault();
if (v != null)
{
photo = v;
return File(photo, "image/jpeg");
}
else
return null;
}
推荐答案
你必须为noimage情况返回另一张图片。有一张漂亮的照片,显示没有可用的图像,并在没有找到图像时返回它
You have to return another image for noimage situation. Have a nice photo that show no image available and return it when no image found
找到一个空白图像(.jpg)并将其存储在一个文件夹中。如果数据库中有空值,则将空白图像转换为字节数组并将其发送到客户端。
Find a blank image(.jpg) and store it in a folder. When you have null value in your Database then convert the blank image to byte array and send it to client.
byte[] testbyte = System.IO.File.ReadAllBytes("filepath");
if (v != null)
{
photo = testbyte;
return File(photo, "image/jpeg");
}
而不是显示错误的图像,您可以显示空白图像,表示图像不可用。
Instead of showing wrong image you can show blank image which will represent image is not available.
这篇关于图像绑定mvc actionresult剃刀视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文