asp.net-mvc-3 – 在Razor / MVC3中显示db的图像
发布时间:2020-05-23 03:21:30 所属栏目:asp.Net 来源:互联网
导读:我在db中有一个表,其中包含以下内容: – CountryID,CountryName和Country Image. 现在我试图在索引中显示图像,我在View中显示以下内容: td @if (item.Image != null) { img src=@Model.GetImage(item.Image) alt=@item.Co
|
我在db中有一个表,其中包含以下内容: – CountryID,CountryName和Country Image. 现在我试图在索引中显示图像,我在View中显示以下内容: <td>
@if (item.Image != null)
{
<img src="@Model.GetImage(item.Image)" alt="@item.CountryName"/>
}
然后在ViewModel中我有: public FileContentResult GetImage(byte[] image)
{
if (image != null)
return new FileContentResult(image,"image/jpeg");
else
{
return null;
}
}
但是我看不到图像正确. 我究竟做错了什么? 提前感谢您的帮助和时间 UPDATE 好,所以我在视图中实现了以下内容: <td>
@if (item.Image != null)
{
<img src="@Url.Action("GetImage","CountryController",new { id = item.CountryID })" alt="@item.CountryName" />
}
</td>
在CountryController中: public ActionResult GetImage(int id)
{
var firstOrDefault = db.Countries.Where(c => c.CountryID == id).FirstOrDefault();
if (firstOrDefault != null)
{
byte[] image = firstOrDefault.Image;
return File(image,"image/jpg");
}
else
{
return null;
}
}
但是当我尝试调试代码时,ActionResult GetImage没有被击中 解决方法两种可能性写一个控制器动作,而给定一个图像ID将返回此图像: public ActionResult GetImage(int id)
{
byte[] image = ... go and fetch the image buffer from the database given the id
return File(image,"image/jpg");
}
接着: <img src="@Url.Action("GetImage","SomeController",new { id = item.Id })" alt="@item.CountryName" />
显然,在您的初始模型中,您不需要Image属性.随后将在控制器操作中检索此操作. 另一种可能性是使用data URI scheme将图像嵌入base64字符串,但是它可能不被所有浏览器广泛支持: <img src="data:image/jpg;base64,@(Convert.ToBase64String(item.Image))" alt="@item.CountryName" /> 在这种情况下,您不需要控制器操作,因为图像直接嵌入到您的标记中作为base64字符串. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 使用Asp.net Mvc缩略图
- asp.net – Visual Studio 2015的项目模板
- MVC .Net Cascade在使用EF Code First Approach时删除
- asp.net-mvc – 你可以更新部分视图而不是全页信息吗?
- asp.net-mvc – ASP.NET MVC中的WebApi [FromUri]是什么?
- asp.net-core – 如何从MVC 6中的ASP.Net 5 Tag Helper访问
- entity-framework – Ninject WebAPI由于DbContext已被处理
- asp.net中的页面卸载事件
- asp.net – MVC 4 – 在局部视图中使用不同的模型
- 页面刷新导致ASP.NET应用程序中重复的POST
推荐文章
站长推荐
- asp.net – 我应该买Obout控件吗?
- asp.net-mvc – 当我用fiddler检查时,VS2013 RTM
- ASP.NET – 上传大文件时如何显示错误页面(超过最
- asp.net – 对于未更改的静态内容,Amazon CloudF
- 在asp.net mvc 3中实现FilterAttribute,IActionF
- asp.net-mvc – ASP.NET MVC RememberMe
- asp.net – HttpRequestBase.UserHostAddress抛出
- asp.net-mvc-3 – 在asp.net MVC3.0中创建GridVi
- asp.net-mvc – 如何将NUnit作为ASP.NET MVC的测
- asp.net-mvc – 如何用asp.net mvc 3和ListBoxFo
热点阅读
