Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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# 如何在ASP.NET Razor中显示数据库中存储的URL图像?_C#_Asp.net_Asp.net Mvc_Database_Razor - Fatal编程技术网

C# 如何在ASP.NET Razor中显示数据库中存储的URL图像?

C# 如何在ASP.NET Razor中显示数据库中存储的URL图像?,c#,asp.net,asp.net-mvc,database,razor,C#,Asp.net,Asp.net Mvc,Database,Razor,所涉及的数据库包括两个实体,它们是int类型的ID,以及text类型的ImagePath。ID保存一个自动递增的数字,ImagePath保存特定图像的URL。目前,我已经用下面的代码显示了数据库中找到的所有图像 Index.cshtml @foreach (var item in Model) {@Html.DisplayFor(modelItem => item.ImagePath) <img src="~/Images/@Html.DisplayFor(mode

所涉及的数据库包括两个实体,它们是int类型的
ID
,以及text类型的
ImagePath
ID
保存一个自动递增的数字,
ImagePath
保存特定图像的URL。目前,我已经用下面的代码显示了数据库中找到的所有图像

Index.cshtml

 @foreach (var item in Model)
    {@Html.DisplayFor(modelItem => item.ImagePath)
    <img src="~/Images/@Html.DisplayFor(modelItem => item.ImagePath)" />
我想显示一个特定的图像,而不是数据库中找到的所有图像。有人能帮我通过
ID
显示数据库中存储的特定URL图像吗?(即,通过.cshtml中调用的ID显示来自
ImagePath
的图像)

非常感谢您的时间和帮助。多谢各位

更新: 写下下面的代码,以便显示
db.image
表中的图像,该表已选择
ImageID
=2来表示存储的相关url图像

ImageController.cs

  public class ImageController : Controller
    {
        private ImageDBContext db = new ImageDBContext();

          public ActionResult Index()
        {
            return View(db.Images.ToList());
        }
Index.cshtml

@foreach (var item in Model)
    {
        @Html.DisplayFor(modelItem => item.ImagePath)
         <img src="~/Images/@Html.DisplayFor(modelItem => item.ImagePath)"/>

您可以使用LINQ返回数据库中的特定文档。假设您的数据列名为
ID
,则可以执行以下操作

public class ImageController : Controller
{
    private ImageDBContext db = new ImageDBContext();

    public ActionResult Index(int intValue){

    var imagesId = db.Images.Where(img -> img.ID == intValue).ToList();

    return View(imagesId);

    }
}
这将返回Id等于intValue的所有图像

另一方面,您似乎只尝试检索一个文档,如果是这样的话,您可以(也应该)使用FirstOrDefault,比如
var-imageWithId=db.Images.FirstOrDefault(img->img.ID==intValue)

编辑更新+评论:

似乎您希望在索引视图中对客户端的信息进行操作。有多种方法可以做到这一点,一种是通过向控制器传回一个值来重新加载页面,该值将向视图传递正确的模型(在您的情况下是图像)

如果希望用模型中的值替换值,可以迭代模型并替换css属性。选择要替换SRC的元素,然后使用

var imagePath = @Html.Raw(Json.Encode(Model.ImagePath));
$(this).attr("src", imagePath);
使用JQuery使用新图像重新加载页面:

$(document).on('event', '.selector', function() {
     //Set your ID to the Id property of the element   
    var idToPass = this.id;
    clicked
    //This will simulate an HTTP request, your action will return your View with your image
window.location.replace('@Url.Action("Index", "Image")?ImageID=' + idToPass) 
});
将事件替换为要触发重新加载的事件<代码>“单击”
例如。将选择器替换为一个类,该类提供了所有希望单击的元素

注意:小心!在锚上使用
('click)
,可以得到有趣的结果


您还可以使用AJAX动态地将信息传递到页面,而无需重新加载。

您只需在查询中过滤图像即可

公共类ImageController:控制器
{
私有ImageDBContext db=新ImageDBContext();
公共行动结果索引()
{
[…确定id的一些代码..]
返回视图(db.Images)
.其中(x=>x.ID=[myId]
.ToList());

}
这个非常宽泛的问题的哪一步会给你带来问题:获取图像的id以采取行动,从DB中逐个id查询项目,以流的形式返回图像,等等?你需要使用
@Url。行动
查看新的更新,也许它为你的助手澄清了我的最终目标。你能看看我写的更新吗?我不明白他完全更新。如果您希望将参数传递给某个方法,则应使用
public ActionResult Index(int-ImageID)
,然后使用您希望传递的任何Id调用Index-method,例如
Index(2)
Index(9001)
使用这些Id检索图像。您希望捕获什么事件以重定向到特定Id?即,您从何处获取Id号?通过连接的服务器从表中获取“Id”。我希望通过从index.cshtml和不是.ImageController.cs.发布了更新。第一部分将足以让您正确使用模型中的所有图像替换SRC,第二部分将帮助您使用新图像重新加载页面(如果您希望这样做)。祝您好运!感谢您的帮助。您能签出我添加的更新吗?
var imagePath = @Html.Raw(Json.Encode(Model.ImagePath));
$(this).attr("src", imagePath);
$(document).on('event', '.selector', function() {
     //Set your ID to the Id property of the element   
    var idToPass = this.id;
    clicked
    //This will simulate an HTTP request, your action will return your View with your image
window.location.replace('@Url.Action("Index", "Image")?ImageID=' + idToPass) 
});