0

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;
    }

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

This would be because at the bottom of the loop, instead of adding items to the list, you're creating a new list with one item in it.

vmMenuItem.PriceList.Add(vmPrices) (and the same for vmCategory and vmMenu) would solve this problem.

3 Comments

sorry, I'm not sure how to change it.
Oh I see. Okay I did this as well but I'll get the nullReference error on the ".PriceList"
that's because you didn't initialised the lists first; have a look at my answer :)
0

Add this at the top

 vmMenuItem.PriceList = new List<PriceViewModel> ;
    vmCategory.MenuItemList = new List<MenuItemViewModel> ;
    vmMenu.CategoryList = new List<CategoryViewModel> ;

and then at the end do

                  vmMenuItem.PriceList.Add(vmPrices );
                }
                vmCategory.MenuItemList.Add(vmMenuItem);
            }
            vmMenu.CategoryList.Add(vmCategory );

You were not adding you were replacing them with new ones

1 Comment

the reason why this will not work is because if I put those things at the top "vmCategory" and "vmMenuItem" have not been introduced in the code yet so they will not be recognized. Thank you for having an input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.