Error when referencing Model

I am very new to this and am doing a little project to get to know how it all works.

So I’m looking to create a header image area on each page but placing the code in the _Layout.cshtml file and attempting to control what image displays according to ViewBag.Title

HomeController.cs

namespace WebSite_Project_1.Controllers
{
    public partial class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public class Headers
        {
            public string HeaderName { get; set; }
            public string PageName { get; set; }
            public int HeaderWidth { get; set; }
            public int HeaderHeight { get; set; }
            public string HeaderType { get; set; }
        }
        public ActionResult HeaderImages
        {
            get
            {
                var model = new List<Headers>();
                model.Add(new Headers { HeaderName = "home", PageName = "Home Page", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
                model.Add(new Headers { HeaderName = "about", PageName = "About", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
                model.Add(new Headers { HeaderName = "contact", PageName = "Contact", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
                model.Add(new Headers { HeaderName = "funnybones", PageName = "Funny Bones", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });

                return View(model);
            }
        }
    }
}

_Layout.cshtml

@model IEnumerable<WebSite_Project_1.Controllers.HomeController.Headers>

<div class="headersImage">
            @foreach (var item in Model)
            {
                if (@item.PageName == @ViewBag.Title)
                {
                    <img src="~/Content/Images/@item.HeaderName+@item.HeaderType" title="@item.HeaderName" />
                }
            }
</div>
@RenderBody()

Im nor sure for starters if the if statement is correct but when I publish I get this message

And maybe the if should be a while instead.

while (@item.PageName == @ViewBag.Title)

I think its because Model is possibly equal to null, but I don’t know how to not make it equal to null. I might be wrong but having looked into the error, it seems its that.

c# is case sensitive so…

model != Model

Hi do you mean that I change var model to var Model and then all references where model has a small m.

Because i did that and it still came up with the same error.

I also noticed that in one case, you’re using List<> and in the other, you’re using IEnumerable<>.

These aren’t the same, and may be causing your issues…

Thats funny I was just about to say i think its this line -

@model IEnumerable<WebSite_Project_1.Controllers.HomeController.Headers>

Im sure in there I should reference Model, or like you say do it another way

So changed it to this

@model List<WebSite_Project_1.Controllers.HomeController.Headers>

And still the same error, but haven;t referenced Model. I’m not sure

Ive gone off peak somewhere and i cant work it out, as I was saying this is my very first steps, so if I need to rip it up then no worries.

Why do you have the code in the ActionResult in a get?

Don’t know :grin:

public ActionResult HeaderImages()
        {
                var model = new List<Headers>();
                model.Add(new Headers { HeaderName = "home", PageName = "Home Page", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
                model.Add(new Headers { HeaderName = "about", PageName = "About", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
                model.Add(new Headers { HeaderName = "contact", PageName = "Contact", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
                model.Add(new Headers { HeaderName = "funnybones", PageName = "Funny Bones", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });

                return View(model);
        }

Leave it like above you mean is it?

Ah I know why, the lightbulb appeared and then it asked me if I wanted to replace HeaderImages with property, i said yes and it changed it for me, to have it inside get and take the brackets away

That’s how mine are coded…

Is it a version issue and something missing to recognize the capitals.

Dragging the barrel here, but am googling it, and cant find a definitive answer, but plenty of suggestions.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.