Azure Blob存储容器作为下拉列表项

问题描述 投票:-2回答:1

我想将azure blob存储容器名称显示为DropDownList的项目。我想用ASP.NET和MVC来做这件事

asp.net asp.net-mvc
1个回答
1
投票

使用WindowsAzure.Storage nuget包,以下代码应该从blob存储中获取容器列表:

public ActionResult Index()
{
   // User your connection string in place of connectionString.
   var storageAccount = CloudStorageAccount.Parse(connectionString);

   // Create the blob client.
   var blobClient = storageAccount.CreateCloudBlobClient();

   // Get the list of containers from blob storage.
   var containers = blobClient.ListContainers();

   // Generate a MVC SelectList from the list of containers.
   var listForView  = new SelectList(containers, "Name", "Name");

   // You can now attach this to your existing view model or just put it in a ViewBag and use it from there on your view
   ViewBag.ContainersList = listForView;

   return View();
}

从这里开始,您可以将通过ViewBag创建的选择列表或视图模型传递给View,然后将其与DropDownList帮助器一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.