我有一个包含22个类别的列表,每个类别大约有8个菜单项,但是我的视图模型最终只包含列表中的最后一个项目。我很难看出问题出在哪里。
在这一点上,我确信问题是在我如何填充视图模型,但我不知道问题在哪里。
视图模型:
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;
}
是的,问题在于您填充视图模型的方式。您必须初始化列表,然后向其中添加项:
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;
}
这是因为在循环的底部,不是向列表中添加项,而是创建一个包含一个项的新列表。
vmMenuItem.PriceList.Add(vmPrices)
(vmCategory和vmMenu也一样)可以解决这个问题。
添加到顶部
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 );
你不是在添加你是在用新的替换它们