I'm trying to fix all the typos all the namespace that might get the error. But I'm not receiving any errors from console or terminal at this point so I can't find the problem. The problem is I'm trying to receive the errors or messages from models and controllers in my Inventory.cshtml
.
Note that I have different layouts (i don't know if it affected by the layouts).
Like for _DashboardLayout and _Layout
in Shared Folder.
Here is the folder path / file paths I have so it can have a better understanding. MVC1 MVC2
so I have Entities for the Inventory.cshtml
which is the Product.cs
using System.ComponentModel.DataAnnotations;
namespace SportsCapstone.Entities
{
public class Product
{
[Key]
public int Id { get; set; }
// public byte[] Image { get; set; } // Storing image as byte array
[Required(ErrorMessage = "Product is required.")]
[MaxLength(20, ErrorMessage = "Max 20 characters allowed")]
public string ProductName { get; set; }
[Required(ErrorMessage = "Brand is required.")]
[MaxLength(20, ErrorMessage = "Max 20 characters allowed")]
public string ProductBrand { get; set; }
[Required(ErrorMessage = "Quantity should not be empty")]
[Range(1, int.MaxValue, ErrorMessage = "Quantity Must be atleast 1.")]
public int Quantity { get; set; }
[Required(ErrorMessage = "Type of Product is required.")]
[MaxLength(20, ErrorMessage = "Max 20 characters allowed")]
public string TypeOfProduct { get; set; }
[Required(ErrorMessage = "Type of Age is required.")]
[Range(1, 60, ErrorMessage = "Age should not be more than 60")]
public string AgeRange { get; set; }
}
}
And then this is the InventoryItem.cs
using System.ComponentModel.DataAnnotations;
namespace SportsCapstone.Models
{
public class InventoryItem
{
[Required(ErrorMessage = "Product name is required.")]
[MaxLength(20, ErrorMessage = "Max 20 characters allowed.")]
public string ProductName { get; set; }
[Required(ErrorMessage = "Product brand is required.")]
[MaxLength(20, ErrorMessage = "Max 20 characters allowed.")]
public string ProductBrand { get; set; }
[Required(ErrorMessage = "Quantity is required.")]
[Range(1, int.MaxValue, ErrorMessage = "Quantity must be a positive number.")]
public int Quantity { get; set; } // Changed to int
[Required(ErrorMessage = "Type of product is required.")]
[MaxLength(20, ErrorMessage = "Max 20 characters allowed.")]
public string TypeOfProduct { get; set; }
[Required(ErrorMessage = "Age range is required.")]
[Range(5, 20, ErrorMessage = "Age range must be between 5 and 20.")]
public string AgeRange { get; set; }
// [Required(ErrorMessage = "Image is required.")]
// public IFormFile Image { get; set; } // For file uploads
}
}
And this is the InventoryController
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SportsCapstone.Models; // This should be your models namespace
using SportsCapstone.Entities; // Keep if you need to reference Product
using Microsoft.EntityFrameworkCore;
namespace SportsCapstone.Controllers // Corrected namespace
{
[Authorize]
public class InventoryController : Controller
{
private readonly AppDbContext _dbContext;
public InventoryController(AppDbContext appDbContext)
{
_dbContext = appDbContext;
}
public IActionResult Inventory()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Inventory(InventoryItem model) // Change to InventoryItem
{
if (ModelState.IsValid)
{
// If needed, convert InventoryItem to Product here
Product product = new Product
{
ProductName = model.ProductName,
ProductBrand = model.ProductBrand,
Quantity = model.Quantity,
TypeOfProduct = model.TypeOfProduct,
AgeRange = model.AgeRange
};
try
{
_dbContext.Products.Add(product);
_dbContext.SaveChanges();
ModelState.Clear();
ViewBag.Message = $"{product.ProductName} registered successfully. Please Login.";
}
catch (DbUpdateException)
{
ModelState.AddModelError("", "Please enter a unique Email or Password.");
return View(model);
}
return View();
}
return View("Inventory", model); //return model with correct view name
}
}
}
then the update in Inventory.cshtml
the AgeRange of input is not int but a string because I will return it to adult or kids
@model SportsCapstone.Models.InventoryItem
@{
Layout = "_DashboardLayout";
}
...
@if (@ViewBag.Message != null)
{
<div class="d-flex align-items-center justify-content-center w-100" style="color: green">
@ViewBag.Message
</div>
}
<form asp-controller="Inventory" asp-action="Create" method="post" class="inv-cp-ppqty">
<div class="image">
<label for="imageUpload">
<svg id="imageLabel" width="16" height="16" viewBox="0 0 16 16" fill="none"
xmlns="http://www.w3.org/2000/svg">
</svg>
<img id="imagePreview"
style="display: none; max-width: 100%; height:100%; object-fit: cover; border-radius: 10px;" />
</label>
@* <input asp-for="Image" type="file" id="imageUpload" name="Image" required
onchange="previewImage(event)" /> *@
@* <span asp-validation-for="Image" class="text-danger"></span> *@
@* <img src="" alt=""> *@
</div>
<div class="fill-inv">
<div class="inp-inv">
<input asp-for="ProductName" type="text" placeholder="Product Name" />
<span asp-validation-for="ProductName" class="text-danger"></span>
</div>
<div class="inp-inv">
<input asp-for="ProductBrand" type="text" placeholder="Product Brand" />
<span asp-validation-for="ProductBrand" class="text-danger"></span>
</div>
<div class="inp-inv">
<input asp-for="Quantity" type="number" placeholder="Quantity" min="1" />
<span asp-validation-for="Quantity" class="text-danger"></span>
</div>
<div class="inp-inv">
<select asp-for="TypeOfProduct" id="typeOfProduct" class="form-control">
<option value="" disabled selected> Type of Product </option>
<option value="Cloth"> Cloth </option>
<option value="Pants"> Pants </option>
<option value="Accessories"> Accessories </option>
<option value="SportsGear"> Sports Gear </option>
<!-- Add more options as needed -->
</select>
<span asp-validation-for="TypeOfProduct" class="text-danger"></span>
</div>
<div class="inp-inv">
<select asp-for="AgeRange" id="ageRange" class="form-control">
<option value="" disabled selected> Age </option>
<option value="Kids"> -18 </option>
<option value="Adults"> 18+ </option>
<!-- Add more options as needed -->
</select>
<span asp-validation-for="AgeRange" class="text-danger"></span>
</div>
<button type="submit" class="button-type">
<h5> Create Item </h5>
</button>
</div>
</form>
Firstly, you need change your select of AgeRange
like below, due to you have validation for this property must be between 5 and 20. Otherwise, the model validation will always be false:
<select asp-for="AgeRange" id="ageRange" class="form-control">
<option value="" disabled selected> Age </option>
<option value="6"> 6 </option>
<option value="7"> 7 </option>
<option value="8"> 8 </option>
<!-- Add more options as needed -->
</select>
Then your view name is Inventory.cshtml
, if you do not return view with specific name, it will return the view name which is same as action name. That is to say you will return to Create.cshtml. Change your action like below:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(InventoryItem model) // Change to InventoryItem
{
if (ModelState.IsValid)
{
//...
return RedirectToAction("Index"); // Adjust as needed
}
return View("Inventory", model); //return model with correct view name
}