【问题标题】:I need just to display the multiple images I insert我只需要显示我插入的多个图像
【发布时间】:2019-04-05 17:13:39
【问题描述】:

我只需要显示我插入的多个图像,它会保存在路径中,但是当他显示时它会显示出来。我不知道我的问题出在哪里,但这是我的代码,请大家帮帮我?

我的控制器

public async Task<ActionResult> Create(Inbox model, IEnumerable<HttpPostedFileBase> files)
{
    var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());
    if (ModelState.IsValid)
    {
        model.User = currentUser;
        foreach (var file in files)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/FilesAPP"), fileName);
                file.SaveAs(path);
                path = Url.Content(Path.Combine("~/FilesAPP", fileName));
            }
        }
        db.Inboxs.Add(model);
        db.SaveChanges();
        string url = Url.Action("List");
        return Json(new { success = true, url = url });
    }
    return View(model);
}

我的模型

public string Files { get; set; }

我的观点

<img width="200" height="150" src="@Url.Content(Model.Files))" />

伙计们,我怎样才能显示我的图片?

【问题讨论】:

  • 首先,检查您的浏览器本地主机路径“localhost:0000/FilesAPP/abc.jpg”检查您在浏览器中键入时显示的图像。如果是,则检查您使用时生成的路径,src="@Url.Content(Model.Files))"

标签: c# asp.net-mvc asp.net-mvc-4 razor asp.net-mvc-5


【解决方案1】:

首先为文件创建一个列表

    public async Task<ActionResult> Create(Inbox model, IEnumerable<HttpPostedFileBase> files)
        {
List<Inbox>lst=new List<Inbox>();
            var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());
            if (ModelState.IsValid)
            {
                model.User = currentUser;
                foreach (var file in files)
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/FilesAPP"), fileName);
                        file.SaveAs(path);
                        path = Url.Content(Path.Combine("~/FilesAPP", fileName));
lst.add(new Inbox{Files=path});
//you files added here
                    }
                }
                db.Inboxs.Add(model);
                db.SaveChanges();
                string url = Url.Action("List");
                return Json(new { success = true, url = url });
            }
            return View(model);
        }

在你看来

@model List<Inbox>

@foreach (var item in Model.Files)
{
    <img src="@item.FilePath" />
}

【讨论】:

    【解决方案2】:

    我看到您正在循环中将文件保存在控制器中,但您没有使用文件路径更新模型。使用控制器中的文件路径更新您的模型,并像您正在做的那样将模型传递给视图。然后,在您的视图中,您还必须执行一个循环,遍历图像并为每个图像渲染一个 img 标签,并将源作为保存在控制器中的文件路径。

    假设您使用的是 Razor ASP.NET 语法,只需在您的视图中执行类似在控制器中执行的操作即可。但请务必先将文件附加到您的模型:

    @foreach (var item in Model.Files)
    {
        <img src="@item.FilePath" />
    }
    

    这是另一篇文章,其中包含 Razor 中的循环示例,祝你好运!

    MVC Razor @foreach

    【讨论】:

    • 您在视图中使用的是 Razor 语法还是标准 ASP.NET 语法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 2012-06-17
    相关资源
    最近更新 更多