我正在创建一个asp.net MVC 4应用程序,我正在尝试显示我上传到文件夹的图像。我已经保存了sql server数据库的路径,每个上传的图像都有自己唯一的ID。我能够在表格中显示Id和路径,但我也希望能够查看图像。
这是它显示的内容:http://imgur.com/a/pPTvG
感谢您对此问题的任何帮助
public partial class Image
{
[Key]
public int ID { get; set; }
public string ImagePath { get; set; }
}
public class HomeController : Controller
{
private IImageRepository repository;
public HomeController(IImageRepository imageRepository)
{
//this.repository = imageRepository;
repository = imageRepository;
}
public ViewResult GetImage(int ID)
{
Image Images = repository.Images
.FirstOrDefault(x => x.ID == ID);
return View(Images);
}
}
@model IEnumerable<MyProject.Models.Image>
@{
ViewBag.Title = "Index";
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.ID)
</th>
<th>
<h5><b>Image</b></h5>
</th>
<th>
@Html.DisplayNameFor(model => model.Heading)
</th>
<th>
@Html.DisplayNameFor(model => model.ImagePath)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td class="text-right">@item.ID</td>
<td>
<img class="img-thumbnail" width="150" height="150" src="@Url.Action("GetImage", "Home",new { item.ID })" />
</td>
<td>
@Html.DisplayFor(modelItem => item.Heading)
</td>
<td>
@Html.DisplayFor(modelItem => item.ImagePath)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@*@Ajax.ActionLink("Delete", "Delete", new { id = item.ID })*@
@Ajax.ActionLink("Delete",
"Delete",
new { id = item.ID },
new AjaxOptions
{
UpdateTargetId = (string)item.ImagePath,
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET"
})
</td>
</tr>
}
</table>
答案 0 :(得分:1)
您的图片来源错误:
<img class="img-thumbnail" width="150" height="150"
src="@Url.Action("GetImage", "Home",new { item.ID })" />
它指向Home/Getimage/id
。你需要设置path
的图像:
如果您已经在模型中使用了imagePath,只需将其绑定到图像源:
<img class="img-thumbnail" width="150" height="150" src="@item.ImagePath" />
答案 1 :(得分:1)
试试这个:
<img class="img-thumbnail" width="150" height="150" src="/folderpath/@item.ImagePath" />