Maximum (M) amount of products shown on the Product page (show only first M products, others – ignored; if M == 0, then show all products)
How can I do this condition? Do I need to do it in ProductPageModel or Product.cshtml?
product.cshtml
@page
@model GetandTake.Pages.ProductModel
@{
}
<table >
<thead>
<tr>
<th scope="col">ProductName</th>
<th scope="col">QuantityPerUnit</th>
<th scope="col">UnitPrice</th>
<th scope="col">UnitsInStock</th>
<th scope="col">UnitsOnOrder</th>
<th scope="col">ReorderLevel</th>
<th scope="col">Discontinued</th>
<th scope="col">Category</th>
<th scope="col">Supplier</th>
</tr>
</thead>
<tbody>
@foreach (var product in Model.Products)
{
<tr>
<td>@product.ProductName</td>
<td>@product.QuantityPerUnit</td>
<td>@product.UnitPrice</td>
<td>@product.UnitsInStock</td>
<td>@product.UnitsOnOrder</td>
<td>@product.ReorderLevel</td>
<td>@product.Discontinued</td>
<td>@product.Category</td>
<td>@product.Supplier</td>
</tr>
}
</tbody>
</table>
ProductModel product.cshtml.cs
public class ProductModel : PageModel
{
private readonly IProductService _productService;
public IEnumerable<ProductsDTO> Products { get; private set; }
public ProductModel(IProductService productService)
{
_productService = productService;
}
public void OnGet()
{
Products = _productService.GetAll();
}
}
CodePudding user response:
if you just need to show the M first Products, just add the .Take()
method from System.Linq
public void OnGet()
{
Products = _productService.GetAll().take(M);
}
Where M
is your max products number.
This is only acceptable if you're NOT seeking for pagination, but just displaying N first products.
As this is a business rule, try to put it in C#
file, not in .cshtml