[英]Image not displaying in view page using Url.Action
我正在尝试在视图页面中显示图像,但未在浏览器中显示。 这是我的代码: MyChat.cshtml
<td>
<img src="@Url.Action("GetImage", "Mailbox", new { name = Session["Loginname"].ToString() })" id="photo"/>
</td>
调节器
public FileContentResult GetImage(string name)
{
byte[] cover = GetImageFromDataBase(name);
ViewData["image"] = cover;
if (cover != null)
{
return File(cover, "image/jpeg");
}
else
{
return null;
}
}
public byte[] GetImageFromDataBase(string name)
{
RegisterLogic logic = new RegisterLogic();
byte[] image = logic.GetImage(name);
return image;
}
我用于从SQL Server 2008 R2检索值的业务逻辑如下:
public byte[] GetImage(string name)
{
con.ConnectionString = str;
cmd.Connection = con;
if (con.State == ConnectionState.Open)
con.Close();
if (con.State == ConnectionState.Closed)
con.Open();
cmd.CommandText = "Select Picture from InspiniaMessage where UserName='" + name + "'";
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
Message m = new Message();
foreach (DataRow dr in dt.Rows)
{
Message mn = new Message();
mn.Picture = (byte[])(dr["Picture"]);
m.Picture = mn.Picture;
}
return m.Picture;
con.Close();
}
}
我没有任何错误,但是图像未显示在视图中。
Url.Action
生成针对控制器的操作的url,它从不调用该操作,而要调用该操作,则必须使用Html.Action()
帮助器。
当你写:
@Url.Action("ActionName","ControllerName")
它以如下形式返回url作为字符串:
localhost/Controller/Action
要执行操作,请使用Html.Action()
:
<img src="@Html.Action("GetImage",
"Mailbox",
new
{
name = Session["Loginname"].ToString()
})"
id="photo"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.