【问题标题】:How to list images from a directory in a div如何在div中列出目录中的图像
【发布时间】:2013-07-25 14:35:36
【问题描述】:

我的问题是这样的:我想从它的目录中挑选图像,与它们的数量无关,并通过 ul/li 将其列在一个 div 中。我在下面尝试过,但它既没有显示目录名称,也没有显示目录有 5 张图片。我在 HomeController 中有这段代码:

public ActionResult ContactUs()
{
    return View();
}

public ActionResult ListImagesProducts()
{

        DirectoryInfo diretorio = new DirectoryInfo(@"~\Content\images\Products\");

        FileInfo[] Arquivos = diretorio.GetFiles("*.*");


        List<string> model = new List<string>();

        foreach (FileInfo fileinfo in Arquivos)
        {
            model.Add(fileinfo.Name);
        }

        var diretory = model;

        return Json(diretory, JsonRequestBehavior.AllowGet);
}

在 ContactUs 视图中,我有以下代码:

@{
    ViewBag.Title = "Three Jay - Produtos";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
<script type="text/javascript">
     $(getImages);
     function getImages() {
         $.getJSON("Home/ListImagesProducts", showImages);
     }

     function showImages(data) {
         for (i = 0; i < data.length; i++) {
             var images = data[i];
             $("#diretoryImages").append("<li>" + images.toString + "</li>");
         }
     }

</script>
<div id = "diretoryImages" />

我做错了什么?你们能帮帮我吗?

【问题讨论】:

    标签: javascript jquery json asp.net-mvc-3


    【解决方案1】:

    你错了,你没有调用函数:

    images.toString
    

    加()

    images.toString()
    

    在这种特殊情况下,您甚至不需要调用 toString(),因为 images 变量已经是字符串:

    $("#diretoryImages").append("<li>" + images + "</li>");
    

    【讨论】:

    • @LucasBorsatto log images 看看是否包含数据
    • 我已将 Response.Write(fileInfo.Name) 添加到我的控制器中,它会显示图像文件名。但我只是 Web 开发的初学者,如何在我的视图中记录图像?
    • @LucasBorsatto 你可以写console.log(images); 或写debugger 并且javascript执行将停止,你将能够使用chrome开发者工具检查元素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    相关资源
    最近更新 更多