在同一个Razor页面的不同部分显示图像

时间:2015-04-03 21:47:38

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

我有一个Azure Blob,其中包含一个包含多个子文件夹的容器。在上一个问题中,他们回答我如何获取其中一个子文件夹以输出其内容而不输出其他子文件夹。到目前为止一切都很好。

现在的问题是:

我有几个部分,例如,DFD,用例,流程图等。这些部分将在屏幕截图中看到。我必须找到一种方法如何在正确的部分下输出每个子文件夹。

我正在使用public ActionResult Index()返回View(blobList)(很快会显示代码)。这个问题是它只显示一个子容器,我必须手动输入它。

有什么方法可以在从blob中获取不同部分时显示不同的图像?

我尝试编写一个方法,根据指定的子文件夹基本上从特定blob获取URL,但我不知道如何将Controller的列表从Controller返回到View。

这是我使用

的一些截图和编码

http://i.imgur.com/fVs1SZk.png [例如在陈述图中应该有前两张图片,在活动图中,剩下的图片]

这是编码:

SystemDesign.cs //型号

公共类SystemDesign {     公共字符串URL {get;组; }

public static SystemDesign returnImageURL(IListBlobItem item)
{

    if (item is CloudBlockBlob)
    {
        var blob = (CloudBlockBlob)item;
        return new SystemDesign { URL = blob.Uri.ToString() };

    }
    return null;
}

}

SystemDesignModel.cs

public class SystemDesignModel
    {
        public SystemDesignModel() : this(null)
        {
            Files = new List<SystemDesign>();
        }

        public SystemDesignModel(IEnumerable<IListBlobItem> list)
        {
            Files = new List<SystemDesign>();

            foreach (var item in list)
                {
                    SystemDesign test = SystemDesign.returnImageURL(item);
                    Files.Add(test);

                }


        }
        public List<SystemDesign> Files { get; set; }
    }

index.cshtml

<!-- other code hidden from brevity -->
@foreach (var item in Model.Files)
{
    <a href="@item.URL"><img src="@item.URL" height="128" width="128" /></a>
}

SystemDesignController索引()

public ActionResult Index()
        {
            // Code until Container reference removed from brevity

            Models.SystemDesignModel blobsListDFD = new Models.SystemDesignModel(storageContainer.ListBlobs("**dfd**", useFlatBlobListing: true));
            return View(blobsListDFD);
        }

这是我尝试发明的方法,以便返回字符串URI列表,但不知道如何从Controller传递给View 在这个方法中我想传递容器名称在Index.cshtml中,然后根据参数,我在所述子文件夹中输出这些图像

public List<string> showBlobs(string blobContainerName)
{
    StorageCredentials credentials = new StorageCredentials("swiftdevelopmentstorage", "HqaCkZjdQ8w/DX/fS3wDxU6HXbeqV5EZ1b+UQaKALxaJDrN9JoZZYn8Q0KT6QR4tCrdGQicxE+tKRKScjINW8w==");
    CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);

    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer storageContainer = blobClient.GetContainerReference("systemdesign");

    List<string> list = new List<string>();
    Models.SystemDesignModel blobsListDFD = new Models.SystemDesignModel(storageContainer.ListBlobs("dfd", useFlatBlobListing: true));
    foreach (var item in blobsListDFD.Files)
    {
        list.Add(item.URL.ToString());
    }


    return list;

}

我有什么可以改变的吗?

1 个答案:

答案 0 :(得分:0)

好的,这很令人尴尬,但我会回答我自己的问题,因为我想出了如何修复它。

我所做的是实施了_PartialView,它与方法showBlobs(string containerName)相关联。 Index.cshtml@foreach中找到的代码被重新定位到部分视图中,因此只会传递所需容器中的图像。

下面我发布所有重新编写的代码,使其工作。希望它对某人有帮助

SystemDesignController.cs //更改了索引()和_showBlobs()

public ActionResult Index()
{
    return View();
}
// Other code

[ChildActionOnly]
public PartialViewResult _showBlobs(string containerName)
{
    StorageCredentials credentials = new StorageCredentials(name, key);
    CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);

    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer storageContainer = blobClient.GetContainerReference(rootContainer);


    Models.SystemDesignModel blobsList = new Models.SystemDesignModel(storageContainer.ListBlobs(containerName, useFlatBlobListing: true));

    return PartialView(blobsList);

}

_showBlobs.cshtml //部分视图

@{
        foreach (var item in Model.Files)
        {
            <a href="@item.URL"><img src="@item.URL" height="128" width="128" /></a>

        }

}

SystemDesignModel.cs //模型和实例

 public partial class SystemDesign
    {
        public string URL { get; set; }


        public SystemDesign() { }

        public SystemDesign (string url, string directory)
        {
            this.URL = url;

        }

        public static SystemDesign returnImageURL(IListBlobItem item)
        {

            if (item is CloudBlockBlob)
            {
                var blob = (CloudBlockBlob)item;
                return new SystemDesign 
                {   URL = blob.Uri.ToString(),

                };

            }
            return null;
        }

    }   


    // System Design Model 
    public partial class SystemDesignModel
    {
        public SystemDesignModel() : this(null)
        {
            Files = new List<SystemDesign>();
        }

        public SystemDesignModel(IEnumerable<IListBlobItem> list)
        {
            Files = new List<SystemDesign>();

            foreach (var item in list)
                {
                    SystemDesign test = SystemDesign.returnImageURL(item);
                    Files.Add(test);

                }


        }
        public List<SystemDesign> Files { get; set; }
    }
相关问题