Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按存储在sql server数据库中的路径显示图像_C#_Asp.net Mvc_Image_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 按存储在sql server数据库中的路径显示图像

C# 按存储在sql server数据库中的路径显示图像,c#,asp.net-mvc,image,asp.net-mvc-4,razor,C#,Asp.net Mvc,Image,Asp.net Mvc 4,Razor,我正在创建一个asp.net MVC 4应用程序,并尝试显示已上载到文件夹的图像。我已将路径保存到sql server数据库,每个上载的图像都有自己的唯一ID。我可以在表中显示ID和路径,但我也希望能够查看图像 这是它显示的内容: 谢谢你在这个问题上的帮助 模型 控制器 看法 @model IEnumerable @{ ViewBag.Title=“Index”; } @ActionLink(“新建”、“创建”) @DisplayNameFor(model=>model.ID) 形象 @Di

我正在创建一个asp.net MVC 4应用程序,并尝试显示已上载到文件夹的图像。我已将路径保存到sql server数据库,每个上载的图像都有自己的唯一ID。我可以在表中显示ID和路径,但我也希望能够查看图像

这是它显示的内容:

谢谢你在这个问题上的帮助

模型 控制器 看法
@model IEnumerable
@{
ViewBag.Title=“Index”;
}

@ActionLink(“新建”、“创建”)

@DisplayNameFor(model=>model.ID) 形象 @DisplayNameFor(model=>model.Heading) @DisplayNameFor(model=>model.ImagePath) @foreach(模型中的var项目) { @item.ID @DisplayFor(modelItem=>item.Heading) @DisplayFor(modelItem=>item.ImagePath) @ActionLink(“编辑”,“编辑”,新的{id=item.id})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.id})| @*@ActionLink(“删除”,“删除”,新的{id=item.id})*@ @ActionLink(“删除”, “删除”, 新的{id=item.id}, 新选择 { UpdateTargetId=(字符串)item.ImagePath, InsertionMode=InsertionMode.Replace, HttpMethod=“获取” }) }
您的图像源错误:

它指向
Home/Getimage/id
。您需要设置图像的路径:

如果模型中已有imagePath,只需将其绑定到图像源:

试试这个:

 <img class="img-thumbnail" width="150" height="150" src="/folderpath/@item.ImagePath" />

  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>
 <img class="img-thumbnail" width="150" height="150" src="/folderpath/@item.ImagePath" />