L
L
lena_shevtsova2021-12-21 15:30:59
ASP.NET
lena_shevtsova, 2021-12-21 15:30:59

How to get class field data if the field is List?

How do I read the ImgUrl field? in MVC
Realty.cs

public class Realty
    {
        public List<string> ImgUrl { get; set; }
        public int Id { get; set; }
        public string Type { get; set; }
        public int? NumberRoom { get; set; }
        public string Address { get; set; }
        public double Square { get; set; }
        public int? Floor { get; set; } //этаж
        public int? Floors { get; set; } //всего этажей
        public string Status { get; set; }
        public int CityId { get; set; }
        public int OwnerId { get; set; }
        public int Price { get; set; }
        public string Description { get; set; }
        public int? RealtorId { get; set; }

    }

database initialization
...
            List<string> urls = new List<string>();
            urls.Add(imgUrl);
            urls.Add(imgUrl);
            
            db.Realties.Add(new Realty { ImgUrl = urls, Floors = 10, Type = Types.APARTMENT, NumberRoom = 1, Square = 34, Floor = 2, Status = Statuses.RENT_OUT, CityId = 1, Address = "Северная, дом 20", OwnerId = 3, Price = 12000, Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus iaculis facilisis. Ut mollis libero magna, ut dapibus mi eleifend sit amet. Nam lacinia sodales tellus, vel tristique nisi auctor quis. Nulla congue mauris ac accumsan laoreet. Proin ac dui in ligula commodo facilisis. Integer pretium velit sollicitudin luctus interdum. Nam ac dui at risus posuere fringilla. Donec ac lacus ligula. Duis blandit lacus et leo euismod gravida. Aliquam leo enim, fermentum vitae felis quis, rutrum fringilla nisl. Aliquam erat volutpat. Nullam mattis magna magna, et fermentum odio imperdiet sed." });
            db.Realties.Add(new Realty { ImgUrl = urls, Floors = 10, Type = Types.APARTMENT, NumberRoom = 3, Square = 70, Floor = 5, Status = Statuses.RENT_OUT, CityId = 1, Address = "Кондарева Ул., дом 2", OwnerId = 3, Price = 30000, Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus iaculis facilisis. Ut mollis libero magna, ut dapibus mi eleifend sit amet. Nam lacinia sodales tellus, vel tristique nisi auctor quis. Nulla congue mauris ac accumsan laoreet. Proin ac dui in ligula commodo facilisis. Integer pretium velit sollicitudin luctus interdum. Nam ac dui at risus posuere fringilla. Donec ac lacus ligula. Duis blandit lacus et leo euismod gravida. Aliquam leo enim, fermentum vitae felis quis, rutrum fringilla nisl. Aliquam erat volutpat. Nullam mattis magna magna, et fermentum odio imperdiet sed." });
            db.Realties.Add(new Realty { ImgUrl = urls, Floors = 12, Type = Types.APARTMENT, NumberRoom = 2, Square = 55, Floor = 7, Status = Statuses.RENT_OUT, CityId = 1, Address = "Маяковского Ул., дом 14", OwnerId = 1, Price = 3000000, Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus iaculis facilisis. Ut mollis libero magna, ut dapibus mi eleifend sit amet. Nam lacinia sodales tellus, vel tristique nisi auctor quis. Nulla congue mauris ac accumsan laoreet. Proin ac dui in ligula commodo facilisis. Integer pretium velit sollicitudin luctus interdum. Nam ac dui at risus posuere fringilla. Donec ac lacus ligula. Duis blandit lacus et leo euismod gravida. Aliquam leo enim, fermentum vitae felis quis, rutrum fringilla nisl. Aliquam erat volutpat. Nullam mattis magna magna, et fermentum odio imperdiet sed." });
            db.Realties.Add(new Realty { ImgUrl = urls, Floors = 2, Type = Types.HOUSE, NumberRoom = 5, Square = 102, Status = Statuses.FOR_RENT_DAILY, CityId = 1, Address = "Маяковского Ул., дом 14", OwnerId = 2, Price = 50000, Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus iaculis facilisis. Ut mollis libero magna, ut dapibus mi eleifend sit amet. Nam lacinia sodales tellus, vel tristique nisi auctor quis. Nulla congue mauris ac accumsan laoreet. Proin ac dui in ligula commodo facilisis. Integer pretium velit sollicitudin luctus interdum. Nam ac dui at risus posuere fringilla. Donec ac lacus ligula. Duis blandit lacus et leo euismod gravida. Aliquam leo enim, fermentum vitae felis quis, rutrum fringilla nisl. Aliquam erat volutpat. Nullam mattis magna magna, et fermentum odio imperdiet sed." });
            db.Realties.Add(new Realty { ImgUrl = urls, Floors = 1, Type = Types.WAREHOUSE, Square = 100, Floor = 1, Status = Statuses.SALE, CityId = 1, Address = "102 Квартал, дом 56", OwnerId = 1, Price = 30000, Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus iaculis facilisis. Ut mollis libero magna, ut dapibus mi eleifend sit amet. Nam lacinia sodales tellus, vel tristique nisi auctor quis. Nulla congue mauris ac accumsan laoreet. Proin ac dui in ligula commodo facilisis. Integer pretium velit sollicitudin luctus interdum. Nam ac dui at risus posuere fringilla. Donec ac lacus ligula. Duis blandit lacus et leo euismod gravida. Aliquam leo enim, fermentum vitae felis quis, rutrum fringilla nisl. Aliquam erat volutpat. Nullam mattis magna magna, et fermentum odio imperdiet sed." });
            db.Realties.Add(new Realty { ImgUrl = urls, Floors = 10, Type = Types.ROOM, Square = 15, Floor = 10, Status = Statuses.RENT_OUT, CityId = 1, Address = "236 Квартал, дом 20", OwnerId = 1, Price = 5000, Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus iaculis facilisis. Ut mollis libero magna, ut dapibus mi eleifend sit amet. Nam lacinia sodales tellus, vel tristique nisi auctor quis. Nulla congue mauris ac accumsan laoreet. Proin ac dui in ligula commodo facilisis. Integer pretium velit sollicitudin luctus interdum. Nam ac dui at risus posuere fringilla. Donec ac lacus ligula. Duis blandit lacus et leo euismod gravida. Aliquam leo enim, fermentum vitae felis quis, rutrum fringilla nisl. Aliquam erat volutpat. Nullam mattis magna magna, et fermentum odio imperdiet sed." });
            db.Realties.Add(new Realty { ImgUrl = urls, Id = 7, Status = Statuses.LEASED, Type = Types.APARTMENT, NumberRoom = 2, Floor = 2, Floors = 10, CityId = 1, Address = "Каштановая Ул., дом 15, кв. 32", OwnerId = 3, Price = 1200000, RealtorId = 3});
            db.Realties.Add(new Realty { ImgUrl = urls, Id = 8, Status = Statuses.LEASED, Type = Types.BUILDING, Floors = 10, CityId = 2, Address = "Учительская Ул., дом 48", OwnerId = 4, Price = 3000000, RealtorId = 3, });
            db.Realties.Add(new Realty { ImgUrl = urls, Id = 9, Status = Statuses.LEASED, Type = Types.GARAGE, Floors = 1, CityId = 3, Address = "Океанский Проспект, дом 10", OwnerId = 5, Price = 3000000, RealtorId = 3, });
            db.Realties.Add(new Realty { ImgUrl = urls, Id = 10, Status = Statuses.LEASED, Type = Types.ROOM, Floor = 2, Floors = 10, CityId = 3, Address = "Океанский Проспект, дом 10, кв. 40", OwnerId = 6, Price = 5000000, RealtorId=1 });
            db.Realties.Add(new Realty { ImgUrl = urls, Id = 11, Status = Statuses.LEASED, Type = Types.BED, Floor = 5, Floors = 10, CityId = 1, Address = "Бабушкиной Н. Ул., дом 16, кв. 147", OwnerId = 1, Price = 3200000, RealtorId=2 });
            db.Realties.Add(new Realty { ImgUrl = urls, Id = 12, Status = Statuses.LEASED, Type = Types.HOUSE, NumberRoom = 5, Floors = 3, CityId = 2, Address = "50-Й Мк-Н, дом 29", OwnerId = 2, Price = 5250000, RealtorId = 1 });

...

getting database data
IList<Realty> realties = db.Realties.Where(r => r.Status == status).ToList();

realties.ImgUrl turns out to be null, although everything is fine during initialization

if you write it like this
object p = db.Entry(db.Realties).Collection(r => r.ImgUrl);

then error CS1061 appears

upd: it turns out ImgUrl is visible only during initialization, and then for some reason ceases to exist

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Voland69, 2021-12-21
@Voland69

I suspect that you need to use Include .
More or less like this:

IList<Realty> realties = db.Realties.Where(r => r.Status == status).Include(r => r.ImgUrl).ToList();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question