Search code examples
asp.net-mvcasp.net-mvc-viewmodelview-model-pattern

Populating mvc4 viewmodel with multiple List<>


I have a list of 22 categories with about 8 menuItems per category, but my viewmodel ends up with only the last item in the list. I'm having a hard time seeing where the problem is.
At this point I'm sure the problem is in how I'm populating the viewmodel but I don't know where the problem is.

The ViewModels:

public class MenuViewModel    

    public List<CategoryViewModel> CategoryList { get; set; }

public class CategoryViewModel
{
    public int CategoryID { get; set; }
    public string CategoryTitle { get; set; }
    public List<MenuItemViewModel> MenuItemList { get; set; }
}

public class MenuItemViewModel
{
    public string Title { get; set; }
    public string Note { get; set; }
    public string Description { get; set; }
    public List<PriceViewModel> PriceList { get; set; }

}

public class PriceViewModel
{
    public decimal PriceValueRegularLunch { get; set; }
    public decimal PriceValueSmallLunch { get; set; }
    public decimal PriceValueLargeLunch { get; set; }

    public decimal PriceValueRegularDinner { get; set; }
    public decimal PriceValueSmallDinner { get; set; }
    public decimal PriceValueLargeDinner { get; set; }

    public decimal PriceValueRegularTakeOut { get; set; }
    public decimal PriceValueSmallTakeOut { get; set; }
    public decimal PriceValueLargeTakeOut { get; set; }
}

private MenuViewModel LoadViewModel(int menuNameID)        
{

        List<Category> returnedCategories = GetAllMenuDataModel.GetAllMenuItemsByCategory(menuNameID);

        MenuViewModel vmMenu = new MenuViewModel();

        PriceViewModel vmPrices = new PriceViewModel();

        foreach (Category category in returnedCategories)
        {
            CategoryViewModel vmCategory = new CategoryViewModel
                                               {
                                                   CategoryID = category.categoryId,
                                                   CategoryTitle = category.categoryTitle
                                               };

            foreach (MenuItem menuItem in category.MenuItems)
            {
                MenuItemViewModel vmMenuItem = new MenuItemViewModel
                                                   {
                                                       Title = menuItem.itemTitle,
                                                       Description = menuItem.itemDescription,
                                                       Note = menuItem.itemNote
                                                   };

                foreach (Price price in menuItem.Prices)
                {
                    switch (price.MealType.mealName.ToLower())
                    {
                        case "lunch":
                            if (price.ServingSize.sizeName == "Regular")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Small")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Large")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            break;
                        case "dinner":
                            if (price.ServingSize.sizeName == "Regular")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Small")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Large")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            break;
                        case "takeOut":
                            if (price.ServingSize.sizeName == "Regular")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Small")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Large")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            break;
                    }

                    vmMenuItem.PriceList = new List<PriceViewModel> { vmPrices };
                }

                vmCategory.MenuItemList = new List<MenuItemViewModel> { vmMenuItem };
            }
            vmMenu.CategoryList = new List<CategoryViewModel> { vmCategory };
        }
        return vmMenu;
    }

Solution

  • Yes, the problem is with the way you are populating your view model. You must initialize the lists and then add items to them:

    private MenuViewModel LoadViewModel(int menuNameID)
    {
        List<Category> returnedCategories = GetAllMenuDataModel.GetAllMenuItemsByCategory(menuNameID);
        MenuViewModel vmMenu = new MenuViewModel();
        vmMenu.CategoryList = new List<CategoryViewModel>();
        foreach (Category category in returnedCategories)
        {
            CategoryViewModel vmCategory = new CategoryViewModel
            {
                CategoryID = category.categoryId,
                CategoryTitle = category.categoryTitle
            };
            vmCategory.MenuItemList = new List<MenuItemViewModel>();
    
            foreach (MenuItem menuItem in category.MenuItems)
            {
                MenuItemViewModel vmMenuItem = new MenuItemViewModel
                {
                    Title = menuItem.itemTitle,
                    Description = menuItem.itemDescription,
                    Note = menuItem.itemNote
                };
                vmMenuItem.PriceList = new List<PriceViewModel>();
    
                foreach (Price price in menuItem.Prices)
                {
                    PriceViewModel vmPrices = new PriceViewModel();
                    switch (price.MealType.mealName.ToLower())
                    {
                        case "lunch":
                            if (price.ServingSize.sizeName == "Regular")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Small")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Large")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            break;
                        case "dinner":
                            if (price.ServingSize.sizeName == "Regular")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Small")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Large")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            break;
                        case "takeOut":
                            if (price.ServingSize.sizeName == "Regular")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Small")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            if (price.ServingSize.sizeName == "Large")
                            {
                                vmPrices.PriceValueLargeLunch = price.priceValue;
                            }
                            break;
                    }
                    vmMenuItem.PriceList.Add(vmPrices);
                }
                vmCategory.MenuItemList.Add(vmMenuItem);
            }
            vmMenu.CategoryList.Add(vmCategory);
        }
        return vmMenu;
    }