Product-enhancements-and-validation-fixes
This commit is contained in:
@@ -20,6 +20,11 @@ public class ProductsController : Controller
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
// Prevent caching of products list to show real-time data
|
||||
Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
Response.Headers.Add("Pragma", "no-cache");
|
||||
Response.Headers.Add("Expires", "0");
|
||||
|
||||
var products = await _productService.GetAllProductsAsync();
|
||||
return View(products);
|
||||
}
|
||||
@@ -34,17 +39,31 @@ public class ProductsController : Controller
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create(CreateProductDto model)
|
||||
{
|
||||
Console.WriteLine($"Received Product: Name='{model?.Name}', Description='{model?.Description}', Price={model?.Price}");
|
||||
Console.WriteLine($"Received Product: Name='{model?.Name}', Description='{model?.Description}', Price={model?.Price}, Stock={model?.StockQuantity}");
|
||||
Console.WriteLine($"CategoryId: {model?.CategoryId}");
|
||||
Console.WriteLine($"Weight: {model?.Weight}, WeightUnit: {model?.WeightUnit}");
|
||||
Console.WriteLine($"ModelState.IsValid: {ModelState.IsValid}");
|
||||
|
||||
// Remove Description validation errors since it's optional
|
||||
ModelState.Remove("Description");
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
Console.WriteLine("Validation errors:");
|
||||
foreach (var error in ModelState)
|
||||
{
|
||||
if (error.Value?.Errors.Count > 0)
|
||||
{
|
||||
Console.WriteLine($" {error.Key}: {string.Join(", ", error.Value.Errors.Select(e => e.ErrorMessage))}");
|
||||
}
|
||||
}
|
||||
|
||||
var categories = await _categoryService.GetAllCategoriesAsync();
|
||||
ViewData["Categories"] = categories.Where(c => c.IsActive);
|
||||
return View(model);
|
||||
}
|
||||
|
||||
await _productService.CreateProductAsync(model);
|
||||
var createdProduct = await _productService.CreateProductAsync(model);
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
@@ -68,6 +87,7 @@ public class ProductsController : Controller
|
||||
WeightUnit = product.WeightUnit,
|
||||
Weight = product.Weight,
|
||||
Price = product.Price,
|
||||
StockQuantity = product.StockQuantity,
|
||||
CategoryId = product.CategoryId,
|
||||
IsActive = product.IsActive
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user