[英]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.