0

22のカテゴリのリストがあり、カテゴリごとに約8つのmenuItemがありますが、ビューモデルはリストの最後のアイテムのみになります。問題がどこにあるのかわからない。
この時点で、問題はビューモデルの設定方法にあると確信していますが、問題がどこにあるのかわかりません。

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

3 に答える 3

2

はい、問題はビューモデルへの入力方法にあります。リストを初期化してから、リストにアイテムを追加する必要があります。

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;
}
于 2013-01-11T15:13:19.880 に答える
1

これは、ループの下部で、リストにアイテムを追加する代わりに、1つのアイテムを含む新しいリストを作成しているためです。

vmMenuItem.PriceList.Add(vmPrices)(そしてvmCategoryとvmMenuについても同じです)この問題を解決します。

于 2013-01-11T15:10:10.943 に答える
0

これを上部に追加します

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

そして最後に

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

あなたはそれらを新しいものと交換していたことを追加していませんでした

于 2013-01-11T15:13:10.283 に答える