Skip to content

Cannot implicitly convert type to system.collection.generic.

lakshmi sowmya

Asked Dec 2, 2013, 11:20 PM EST1 answer
Share
LinkedInX
Hi In our MVC application we need to dispaly data from database in nested repeater..Categories in parent repeater and SubCategories in child repeater.
I tried doing it as follows.

Model:
C#
 public class VendorRegistrationStep3
    {
        public int Category_Id { get; set; }
        public string Category_Name { get; set; }
        public string Category_Code { get; set; }
        public string Description { get; set; }
        public bool delete_flag { get; set; }
        public IEnumerable subCategoriesList { get; set; }

    }
    public class VendorRegistrationStep3List
    {
        public IEnumerable categoriesList { get; set; }
    }

    public class subCategory
    {
        public int Subcategory_Id { get; set; }
        public string Subcategory_Name { get; set; }
        public string Subcategory_Code { get; set; }
        public int categoryId { get; set; }
        public string Description { get; set; }
        public bool delete_flag { get; set; }
    }

Controller :

[HttpGet]
C#
        public ActionResult VendorRegistrationStep3()
        {
            var model = GetCategories();
            return View(model);
        }


        public static IEnumerable GetCategories()
        {
            // Make sure my context gets disposed as soon as I'm done with it.
            IEnumerable categoryList;
            using (var db = new bid_portalEntities())
            {
                // Pull all the questions and answers out in a single round-trip
              var Categories = (from objcat in db.bp_category
                                  select new VendorRegistrationStep3
                                  {
                                      Category_Code = objcat.Category_Code,
                                      Category_Name = objcat.Category_Name,
                                      Category_Id = objcat.Category_Id,
                                      subCategoriesList = (from objSubcat in db.bp_subcategory
                                                           where objcat.Category_Id == objSubcat.categoryId
                                                           select new subCategory
                                                           {
                                                               Subcategory_Name = objSubcat.Subcategory_Name,
                                                               Subcategory_Id = objSubcat.Subcategory_Id,
                                                               Subcategory_Code = objSubcat.Subcategory_Code
                                                           })
                                  });


              categoryList = Categories.AsEnumerable().ToList();
            }


            return new VendorRegistrationStep3List{ categoriesList = categoryList };---------Getting Error here
        }


View:

 
                                @model IEnumerable
                                @foreach (var id in Model)
                                {
                                   
                                       
                                           
                                               
                                                @*
                                                                CommandArgument='<%#Eval("Category_Id") %>' Visible="true" Height="16px" CommandName="_Show"
                                                                OnClick="PlusBT_OnClick" />*@
                                           
                                           
                                               
                                                    id


I am getting error near Return statement in controller i.e " Cannot implicitly convert MVC.VendorREgistrationStep3list to System.Collections.generic.Ienumearble<MVC.VendorREgistrationStep3list>an explicit conversion exists(are you missing any cast)..
I am new to mvc please help me.....

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Dharmendra Singh
    0
    Modify your query like

                using (var db = new bid_portalEntities())
                {
                    // Pull all the questions and answers out in a single round-trip
                  var Categories = (from objcat in db.bp_category
                                      select new VendorRegistrationStep3
                                      {
                                          Category_Code = objcat.Category_Code,
                                          Category_Name = objcat.Category_Name,
                                          Category_Id = objcat.Category_Id,
                                          subCategoriesList = (from objSubcat in db.bp_subcategory
                                                               where objcat.Category_Id == objSubcat.categoryId
                                                               select new subCategory
                                                               {
                                                                   Subcategory_Name = objSubcat.Subcategory_Name,
                                                                   Subcategory_Id = objSubcat.Subcategory_Id,
                                                                   Subcategory_Code = objSubcat.Subcategory_Code
                                                               }).AsEnumerable().ToList()
                                      });




    hope this help you
    0