0

I am trying to create a View from my Controller, which looks like this.

namespace NorthernDog.Controllers
{
    public class GalleryController : Controller
    {
        // GET: Gallery
        public ActionResult Index()
        {
            DataTable dt = new DataTable();
            dt = Data.DataAccess.GetDogs();
            Models.Gallery gallery = new Models.Gallery();


            foreach (DataRow item in dt.Rows)
                gallery.Dogs.Add(new Models.Dog()
                {
                    Id = Convert.ToInt32(item["Id"]),
                    Name = Convert.ToString(item["Name"]),
                    Type = Convert.ToString(item["Type"]),
                    imagePath = Convert.ToString(item["ImagePath"]),
                    Breeder = Convert.ToString(item["Breeder"]),
                    State = Convert.ToString(item["State"]),
                    ZipCode = Convert.ToString(item["ZipCode"]),
                    BirthDate = Convert.ToDateTime(item["BirthDate"]),
                });

            return View(gallery.Dogs.AsEnumerable());
        }
    }
}

My Dog Model looks like this:

namespace NorthernDog.Models
{
    public class Dog
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string imagePath { get; set; }
        public string Breeder { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
        public List<string> ImageList { get; set; }
        public DateTime BirthDate { get; set; }
    }
}

And lastly my Gallery Model looks like this:

namespace NorthernDog.Models
{
    public class Gallery : IEnumerable
    {
        List<Dog> dogs = new List<Dog>();

        public List<Dog> Dogs
        {
            get { return dogs; }
            set { dogs = value; }
        }

        public Gallery()
        {
        }

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}

So with all that when I try to create the View by right clicking on the Index from the controller and selecting the Gallery as the model I get a

The model item passed into the dictionary is of type 'System.Collections.Generic.List'1[NorthernDog.Models.Dog]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'1[NorthernDog.Models.Gallery]'

I have tried converting my list to an IEnumerable but haven't been able to get the syntax right. What am I missing?

My question just got marked as a duplicate and maybe it is but I just looked at the other post and it doesn't make any sense to me. So if it is the same problem I don't understand it either and it is not helping me solve my issue.

Buck Hicks
  • 1,534
  • 16
  • 27
  • The error message is self explanatory - your passing `IEnumerable` to a view which has `@model IEnumerable`. Change the view to `@model IEnumerable`. But your `Gallery` model makes no sense anyway. Why would you have it implement `IEnumerable`? –  Mar 18 '17 at 00:21
  • make up your mind if you want the Galery class as your model or if you want IEnumerable – DarkSquirrel42 Mar 18 '17 at 00:22
  • @StephenMuecke - I have tried to make this work so many different ways I am not sure which pieces to leave in and which to take out. I have not been able to find one complete sample of what I am trying to do so I am completely lost. – Buck Hicks Mar 18 '17 at 00:29
  • @DarkSquirrel42 I just want a list of Dogs in my View. As I mentioned in previous comment I can't find a sample that doesn't use Entity Framework or some other detail different then how I am doing this, – Buck Hicks Mar 18 '17 at 00:32
  • 1
    Based on what you have shown, your `Gallery` class is not required (and makes no sense anyway). The code in the GET method needs to initialize a new `List` and in your loop, add the new instances of `Dog` to that collection, then return that collection to a view which has `@model IEnumerable` and use a `foreach(var dog in Model)` to display the properties of each item –  Mar 18 '17 at 00:32
  • @StephenMuecke thank you. Your explanation helped me get this resolved. – Buck Hicks Mar 18 '17 at 00:40

0 Answers0