Get all Media Items for a space ASP.Net Core MVC

Okay well I thought that was the piece I needed but itsn’t not. I have been looking around for a solution and regarless of what I have tried I can’t seem to get anywhere. Was wondering if someone could give me some direction here. I know its something with your system that I am just not understanding how they fit together. You guys I don’t believe are doing anything wrong but I know there is a small piece I am trying to make work.
My Goal
I need to pull my Author Model and render the content on the page for that as well as I want to target SPECIFIC items in the Media and put them where I want them on a page. I thought using a viewModel would get me what I wanted. Any direction you can give me would be amazing. Trying to learn Contentful so that I can start working more heavily with it.

Github Repo (https://github.com/Moser2010/WhoAmI) on the test branch for this issue.
MVC

Model
using Contentful.Core.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ContentfulMVC.Models;
using Contentful.Core.Models;

namespace ContentfulMVC.Models.ViewModels
{
public class WhoIAm
{
public Author Author { get; set; }
public List Assets { get; set; }//This doesn’t seem to refrence all the media images
}
}

Controller
public async Task WhoIAm()
{
WhoIAm AAI = new WhoIAm();
AAI.Author = await _client.GetEntry(“56WvqVr1oIOGqScqoY6EYK”);
var Assets = await _client.GetAssets();
return View(“WhoIAm”, AAI);
}
View
@model ContentfulMVC.Models.ViewModels.WhoIAm
< img class=“hImage” src="@Model.Assets.File.URL" />
< contentful-image asset-id=“5na3PJ1om4eyAQcmia0S8y” />

Hi Cody!

Using a ViewModel like this is perfectly fine and one of the usages the SDK was designed for. There’s just one thing you need to update, otherwise, the code looks good.

When you want to fetch related content (as in assets that relate to an entry for example) you must use the collection endpoint GetEntries instead of the single endpoint GetEntry. Only the collection endpoints contain reference content.

If you change your controller to this:

public async Task WhoIAm()
{
    WhoIAm AAI = new WhoIAm();
    var qb = QueryBuilder<Author>.New.FieldEquals("sys.id", "56WvqVr1oIOGqScqoY6EYK");
    AAI.Author = (await _client.GetEntries(qb)).FirstOrDefault();
    return View("WhoIAm", AAI);
}

In your view it’d be preferable to use a <contentful-image>, but this requires you to have added Contentful.AspNetCore to your ViewImports.cshtml. You could of course use a regular image tag as well, but you’d lose some of the nice features of the taghelper. I’ll show both approaches below:

<contentful-image url="@Model.Author.Assets.FirstOrDefault()?.File.Url">
<img src="@Model.Author.Assets.FirstOrDefault()?.File.Url" />

Note that I have made the assumption that your Author class contains a property public List<Asset> Assets { get; set; }. But it seems that maybe this should be the commented out Avatar property from looking at your repo? In that case, the code would look like this:

<contentful-image url="@Model.Author.Avatar?.File.Url">
<img src="@Model.Author.Avatar?.File.Url" />

Using the collection endpoint should make sure your asset list is populated correctly and that you can render all content for the particular entry.

Hope this helps.

Best
Robert