help
Root Question Message
[HttpGet]
attribute to achieve the routing you want. For example[HttpGet("{catId}/{prodId}")]
public async Task<IActionResult> GetProduct(int catId, int prodId)
{
// ...
}
[Route]
is more absolute and doesn't specify the method, it should only ever be used on controller classes to define the base route for the action methods[HttpGet]
[Route("")]
//[Route("Catalog/Index/{categorySelected:int}/{productTypeSelected:int}")] -- this does not work yet
[Route("Catalog/Index/{brandSelected:int}")] // Brand belongs to nothing, we only want brand for the visual display of brands available in any given category
public async Task<IActionResult> Index(int? categorySelected, int? productTypeSelected, int? brandSelected, int? page)
{
int pageSize = 3; // Page size, temporary. Not sure where to put this.
int totalProductCount = (await _services.GetAllProducts()).Count();
IndexViewModel viewModel = new IndexViewModel
{
Products = await _services.GetProducts(productTypeSelected, brandSelected, page ?? 1, pageSize),
ProductType = await _services.GetAllProductTypes(),
Brand = await _services.GetAllBrands(),
Category = await _services.GetAllCategories(),
PaginationHelper = new PaginationHelper()
{
Page = page ?? 1,
ProductCount = totalProductCount,
PageCount = (int)Math.Ceiling(((decimal)totalProductCount / pageSize)),
}
};
viewModel.PaginationHelper.NextIsEnabled = ((page ?? 1) < viewModel.PaginationHelper.PageCount) ? true : false;
viewModel.PaginationHelper.PreviousIsEnabled = (page > 1) ? true : false;
return View(viewModel);
}
[HttpPost]
public async Task<IActionResult> Index(IndexViewModel vm, int? page)
{
// This is where we do validation
return RedirectToAction("Index",
new { categorySelected = vm.CategorySelected,
productTypeSelected = vm.ProductTypeSelected,
brandSelected = vm.BrandSelected,
page = page,
});
}
[Route]
attributes?[HttpGet]
?[HttpGet]
[Route("")]
//[Route("Catalog/Index/{categorySelected:int}/{productTypeSelected:int}")] -- this does not work yet
[Route("Catalog/Index/{brandSelected:int}")]
[HttpGet("Catalog/Index/{brandSelected:int}")]
[Route]
this timeHttpGet
on the actionapp.MapControllerRoute(
name: "default",
pattern: "{controller=Catalog}/{action=Index}/{id?}");