使用多个List<>填充mvc4 viewmodel

时间:2013-01-11 15:06:02

标签: asp.net-mvc asp.net-mvc-viewmodel view-model-pattern

我有一个22个类别的列表,每个类别约有8个menuItem,但我的viewmodel最终只有列表中的最后一个项目。我很难看到问题所在。
在这一点上,我确定问题在于我如何填充视图模型,但我不知道问题出在哪里。

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 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

这是因为在循环的底部,而不是项添加到列表中,而是创建一个包含一个项目的新列表。

vmMenuItem.PriceList.Add(vmPrices)(和vmCategory和vmMenu相同)可以解决这个问题。

答案 2 :(得分: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 );

你没有添加你用新的替换它们