2

First time posting.

I'm fairly new to programming and I'm trying to create a website to house the web apps I build. I currently have a model, view, and controller to project the contents of my Apps table. These apps are hosted elsewhere, so the tiles that appear in the screenshot below simply link out to other locations. I'd like to include a way to display recently used apps in a drawer, (the code housed in the _Layout).

_Layout Drawer:

_Layout Drawer

I'd like to start by displaying ANY model data in the _Layout drawer. If I simply throw my Apps View code into the _Layout, like so:

@_Layout, located beneath navbar code and above RenderBody()@
@model IEnumerable<Apps>
<div id="footerSlideContainer">
    <div id="footerSlideButton"></div>
    <div id="footerSlideContent">
        <div id="footerSlideText">

            <div class="parenttile">

                @foreach (var Apps in Model)
                {
                    <a href="http://@Apps.AppLink" target="_blank">
                        <div class="tile">
                            <div></div>
                            <div class="tilemid">
                                <div></div>
                                <div>
                                    <img class="tileimage" src="@Apps.AppImage" alt="@Apps.AppName" />
                                </div>
                                <div></div>
                            </div>
                            <div class="tilebot">
                                @Apps.AppName
                            </div>
                        </div>
                    </a>
                }
            </div>
            <h3>Recently Used Apps</h3>
            <p>This section will store all of your most recently used apps. It stays on the screen until you click the drawer icon.</p>
        </div>
    </div>
</div>

...I get the following error in Debug on the @foreach line: NullReferenceException: Object reference not set to an instance of an object. .net core. I researched that error here: What is a NullException error and how do I fix it?

  1. I understand the action method comes before the model displays. If I were to use the above code, I would presume that I need to inject the model into the action method - but, does the _layout have a controller?
  2. I researched here: ASP MVC Razor Pass model to view . This looks like the closest solution, but would appreciate someone walking me through it.
  3. Finally, I noticed that I could use a ViewComponent ViewComponent in ASP.NET Core. I could use some help implementing this solution.

Which of these solutions would work/work best?

Then there's the issue of creating the favorites. I think that's a bit more straightforward, as this Display recently viewed items contains all the info I think I need.

Thanks for any help - I'm including my model and controller code below:

Model:

    public class Apps
{

    public int AppID { get; set; }
    public string AppName { get; set; }
    public string AdGroup { get; set; }
    public string AppDescription { get; set; }
    public string AppLink { get; set; }
    public string AppImage { get; set; }

}

Controller (using hard coded, dummy data for now, before I move to db)

public class AppsController : Controller
{
    private List<Apps> _apps;

    public AppsController()
    {
        _apps = new List<Apps>();

        //creating test Apps model 
        _apps.Add(new Apps
        {
            AppID = 1,
            AppName = "Test App 1",
            AdGroup = "Group 1",
            AppDescription = "First test app.",
            AppLink = "www.google.com",
            AppImage = "/images/image1.png"

        }); //et al
    }


    public IActionResult Index()
    {
        return View(_apps);
    }
}

0 Answers0