Spionred
Spionred

Reputation: 807

Using a controller for Blob Storage

I have been following a couple of tutorials posted in this thread using-azure-blobs-with-azure-website

The trouble I am having is neither tutorial go into much detail around how you display the contents of a Blob container.

My goal is to create a container for each record in a list of assets to upload files to. When you go to the Detail View of an asset you will see a list of the files in the container for that Asset and a form to upload more files.

I have created an AssetsDocs controller (with no model) to handle the action to upload the files and added the form to the Asset detail view to select the file and call the upload action. This works in terms of stepping through the code (I have not published it to Azure yet).

For viewing the files I have tried to add the code to get the list of files in the Details Action of the Asset controller but I can't figure out how to pass the list into the view. This is the Details action code in the Asset Controller:

// GET: Assets/Details/5
public ActionResult Details(int? id)
{
    // Retrieve a reference to a container 
    CloudBlobContainer blobContainer =
        _myBlobStorageService.GetCloudBlobContainer();

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

    // Loop over blobs within the container and output the URI to each of them
    foreach (var blobItem in blobContainer.ListBlobs())
        docs.Add(blobItem.Uri.ToString()); 

    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Asset asset = db.Assets.Find(id);
    if (asset == null)
    {
        return HttpNotFound();
    }

    return View(asset);
}

My other thought is to add the code to get the files list in an Index action of the AssetDocs controller and create a partial view, however I can't figure out how to create a partial view and pass the list from the controller and then call it from the Detail view of the Asset.

This is the Index action in the AssetDoc Controller:

    // GET: AssetDocs
    public ActionResult Index()
    {
        // Retrieve a reference to a container 
        CloudBlobContainer blobContainer =
            _myBlobStorageService.GetCloudBlobContainer();

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

        // Loop over blobs within the container and output the URI to each of them
        foreach (var blobItem in blobContainer.ListBlobs())
            docs.Add(blobItem.Uri.ToString());

        return View(docs);
    }

When I create the Index view with no model the page is blank and I can't see to reference docs.

So, is it possible to pass the list to the Detail view in the Asset Controller if so how? or If I go down the partial view, how to I pass the list to an index view and call it from the Asset Detail view?

Upvotes: 0

Views: 1030

Answers (1)

Rick Rainey
Rick Rainey

Reputation: 11256

Normally you would create the view with a model. At least that's how I've always done it. But, lacking that, you could just use the dyanamic ViewBag variable to pass this to the view. I personally don't like this approach (would prefer a strongly typed model) but it is pretty handy.

So, in your Asset controller, you can store the docs into a property of ViewBag and return the view as you normally would.

ViewBag.Docs = docs;

In your Razor view, you could do something like this to display the files.

<ul>
    @foreach (string doc in ViewBag.Docs)
    {
         <li>@doc</li>
    }
</ul>

Upvotes: 1

Related Questions