locked
Problem with EF6 Query and Partialview RRS feed

  • Question

  • User-173651909 posted

    I'm using MVC5 and EF6 and I'm having trouble with a query in my controller. It should return a partialview of the query results. Debugging shows that it's hitting the controller and passing the correct value, but it seems the issue is with the base_components query itself.

    // GET: myPartialView ParitalView
            public PartialViewResult myPartialView(string selected)
            {
                int id = (from i in db.podmaster
                          where i.Model == selected
                          select i.ID).FirstOrDefault();
    
                // I want to return records in db.components where db.device_components.PodID == id above 
                var base_components = (from c in db.components
                                       join dc in db.device_components
                                       on c.ID equals dc.PodID
                                       where dc.PodID == id
                                       select new
                                       {
                                           c.Vendor_SKU,
                                           c.Description,
                                           c.Watts,
                                           c.Kg,
                                           c.Lbs,
                                           c.CategoryID
                                       }).OrderBy(c => c.CategoryID);
                                       
                return PartialView("myPartialView",base_components.ToList());
            }

    If I remove the query and specify a different partial then it returns successfully.

    Thanks in advance
    Adam

    Monday, November 30, 2015 3:46 PM

Answers

  • User-271186128 posted

    Hi Adam,

    According to your code and description, I suppose perhaps the error is related to the Model. The returned records is not match the components model.

    I suggest you could create a ViewModel to display to the result. Like this:

    Code in model

        public class ComponentsViewModel
        {
            public string Vendor_SKU { get; set; }
            public string Description { get; set; }
            public string Watts { get; set; }
            public string Kg { get; set; }
            public string Lbs { get; set; }
            public string CategoryID { get; set; }
    
        }

    Code in controller:

                var base_components = (from c in db.components
                                       join dc in db.device_components
                                       on c.ID equals dc.PodID
                                       where dc.PodID == id
                                       select new ComponentsViewModel
                                       {
                                          Vendor_SKU = c.Vendor_SKU,
                                          Description = c.Description,
                                          Watts = c.Watts,
                                          Kg = c.Kg,
                                          Lbs = c.Lbs,
                                          CategoryID = c.CategoryID
                                       }).OrderBy(c => c.CategoryID);
    
                 return PartialView("myPartialView",base_components.ToList());

    Code in PartialView:

    @model IEnumerable<MVCTestSample.Models.ComponentsViewModel>

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 1, 2015 2:40 AM

All replies

  • User-1672470423 posted

    Hi,

    Can you check using debugger whether base_components is populated with some value after the join.

    If yes you need to check your view how you are accessing the returned list. It may be the problem is with the view.

    Thanks,

    Sumit. 

    Monday, November 30, 2015 5:41 PM
  • User-271186128 posted

    Hi Adam,

    According to your code and description, I suppose perhaps the error is related to the Model. The returned records is not match the components model.

    I suggest you could create a ViewModel to display to the result. Like this:

    Code in model

        public class ComponentsViewModel
        {
            public string Vendor_SKU { get; set; }
            public string Description { get; set; }
            public string Watts { get; set; }
            public string Kg { get; set; }
            public string Lbs { get; set; }
            public string CategoryID { get; set; }
    
        }

    Code in controller:

                var base_components = (from c in db.components
                                       join dc in db.device_components
                                       on c.ID equals dc.PodID
                                       where dc.PodID == id
                                       select new ComponentsViewModel
                                       {
                                          Vendor_SKU = c.Vendor_SKU,
                                          Description = c.Description,
                                          Watts = c.Watts,
                                          Kg = c.Kg,
                                          Lbs = c.Lbs,
                                          CategoryID = c.CategoryID
                                       }).OrderBy(c => c.CategoryID);
    
                 return PartialView("myPartialView",base_components.ToList());

    Code in PartialView:

    @model IEnumerable<MVCTestSample.Models.ComponentsViewModel>

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 1, 2015 2:40 AM