0

商品をカテゴリ別に表示したい。私はlinqを使用していますが、カテゴリに関係なくすべてのアイテムを表示できないことを除いて、すべてが機能します。おそらくルーティングの問題です。私が使用している linq 式についてはすでに質問しているため、問題ないはずです。とにかく、ここに私のコードがあります:

public ViewResult Content(string category, int page = 1)
    { 
         var model = new StoreContentViewModel
            {
             Items = _itemsRepository.GetItems()
                             .Where(i => i.Product.Category == null || 
                                    i.Product.Category != null && i.Product.Category == category)
                             .OrderBy(i => i.ItemId)
                             .Skip((page - 1) * PageSize)
                             .Take(PageSize)
                             .Select(i => i),
             }
     }

RegisterRouts メソッドの内容:

  // without categories
        routes.MapRoute(
            null,
            "Store/Content/Page{page}",
            new { controller = "Store", action = "Content", category = (string)null},
            new { page = @"\d+" });

        // with categories
        routes.MapRoute(
            null,
            "Store/Content/{category}/Page{page}",
            new { controller = "Store", action = "Content" },
            new { page = @"\d+" });

        // Default route
        routes.MapRoute(
            null,
            "{controller}/{action}",
            new { controller = "Home", action = "Index"});

「カテゴリありとカテゴリなし」ルートの順序について混乱しています。

問題は、URLを入力するときです:

~/Store/Content

また:

~/Store/Content/Page1      // or Page2

アイテムは表示されません。しかし、私が入力した場合:

~/Store/Content/Man's-Shoes/Page1   

「メンズ・シューズ」カテゴリーに関連する商品が表示されます。

それで、この問題は敗走と関係がありますか、それとも別の問題がありますか??

ps 私は過去 2 日間この問題に取り組んできたので、どんな助けも適切です。

編集済み: また、失敗するこの単体テストもあります。おそらく書き方が悪いのでしょう。こちらもチェック。
私のモデルには、 Products エンティティと Shipping エンティティを「含む」 Items エンティティがあります。

[TestMethod]
    public void Can_Filter_Content()
    {            
        //Arrange
        private Mock<IItemsRepository> _itemsMock = new Mock<IItemsRepository>();
        private Mock<IProductRepository> _productsMock = new Mock<IProductRepository>();
        private Mock<IShippingRepository> _shippingMock = new Mock<IShippingRepository>();

        _itemsMock.Setup(i => i.GetItems()).Returns(new[]
            {
                new Item { ItemId = 1, ProductId = 1, ShippingId = 1},
                new Item { ItemId = 2, ProductId = 2, ShippingId = 2},
                new Item { ItemId = 3, ProductId = 3, ShippingId = 3},
                new Item { ItemId = 4, ProductId = 4, ShippingId = 4}
            });
        _productsMock.Setup(p => p.GetProducts()).Returns(new[]
            {
                new Product { ProductId = 1, Category = "shoes"},
                new Product { ProductId = 2, Category = "shoes"},
                new Product { ProductId = 3, Category = "superShoes"},
                new Product { ProductId = 4, Category = "shoes"}
            });

        var controller = new StoreController(_itemsMock.Object, 
            _productsMock.Object, _shippingMock.Object) {PageSize = 2};

        // Act
        Item[] result = ((StoreContentViewModel) controller.Content(null).Model).Items.ToArray();
        ViewResult viewResult = controller.Content(null);

        // Assert
        Assert.IsTrue(result[0].ItemId == 1 && result[1].ItemId == 2);
        Assert.AreEqual(result.Length, 2);

        Assert.AreEqual("", viewResult.ViewName);
    }

マビーこれは役に立ちます

4

1 に答える 1

1

これは:

.Where(i => i.Product.Category == null || 
      i.Product.Category != null && i.Product.Category == category)

これである:

.Where(i => category == null || 
      i.Product.Category != null && i.Product.Category == category)

カテゴリが null の場合、元の条件は製品カテゴリが null ではなく、そのカテゴリが null に一致することを示しますが、これは機能しません。新しい条件は、カテゴリが null の場合は条件を評価しないことを示しています。それ以外の場合は、カテゴリ (「紳士靴」) で一致します。

于 2012-12-28T18:37:39.040 に答える