Search code examples
asp.net-mvcrazorflickr

Flickr/FlickrNet results to Razor page


I'm working in .NET Core with the FlickrNet NuGet package, which I use to retrieve images from Flickr:

PhotoCollection photos = flickr.PhotosSearch(options);

The above successfully retrieves a collection of photos. However I struggling to work out how to pass these to the Razor View. Normally one returns a view with a model, but here I want to return a generic collection. How should I go about this?

@using FlickrNet
@List<PhotoCollection> photos

How do I return the PhotoCollection to the Razor view for display?


Solution

  • In the source code we can see that PhotoCollection extends PagedPhotoCollection which extends System.Collections.ObjectModel.Collection<Photo>

    So ultimately your model is Collection<Photo>.

    Your view would then be

    @using FlickrNet
    @model PhotoCollection
    
    <div>Page Number: @Model.Page</div>
    
    @foreach(var photo in Model)
    {
        <div>@photo.PhotoId</div>
        <div>@photo.Title</div>
        ...
    }
    

    And your controller

    public ActionResult GetPhotos()
    {
        PhotoCollection photos = flickr.PhotoSearch(options);
    
        return View(photos);
    }