Click here to Skip to main content
15,936,887 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm getting following error.
EntityType 'SelectListItem' has no key defined. Define the key for this EntityType.

My model is as follows :
    public class DemoTesting
        public int ID { get; set; }
        public string City { get; set; }

        public List<SelectListItem> lstCity { get; set; }

        public DemoTesting()
        { }

        public DemoTesting(List<SelectListItem> lst)
            this.lstCity = lst;


Controller code is:
public ActionResult DemoDropDown()
            List<SelectListItem> lstmenu = new List<SelectListItem>();
            var city = (from items in ContextObj.demoContextObj
                        select items).ToList();
            foreach (var item in city)
                lstmenu.Add(new SelectListItem { Text = item.City.ToString(), Value = item.ID.ToString() });
            DemoTesting obj = new DemoTesting(lstmenu);
            return View(obj);

Im getting this error when trying to get cites by LINQ.Please help

What I have tried:

As per others site i'm already added [Key] attribute in my model but still getting this error.
phil.o 20-Apr-16 5:22am    
You added the [Key] attribute on the DemoTesting class, not on the SelectListitem class, which seems to need it.
Member 12141812 20-Apr-16 5:26am    
So where i need to add [Key] attribute.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900