Вот мой код для первого 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; }
}
}
Код: Выделить всё
using Microsoft.EntityFrameworkCore;
namespace SportsCapstone.Entities
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions options)
: base(options) { }
...
public DbSet
Products { get; set; } // Add this line
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
....
// Configure the Product entity
modelBuilder.Entity()
.Property(p => p.ProductName)
.IsRequired();
modelBuilder.Entity()
.Property(p => p.ProductBrand)
.IsRequired(); // Example for Brand
modelBuilder.Entity()
.Property(p => p.Quantity)
.IsRequired(); // Example for Quantity
modelBuilder.Entity()
.Property(p => p.TypeOfProduct)
.IsRequired(); // Example for Quantity
modelBuilder.Entity()
.Property(p => p.AgeRange)
.IsRequired(); // Example for Quantity
// modelBuilder.Entity()
// .Property(p => p.Image)
// .IsRequired(); // Add this line if you want to ensure the Image property is required
// Continue adding other configurations as necessary...
base.OnModelCreating(modelBuilder);
}
}
}
Код: Выделить всё
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
namespace SportsCapstone.Controllers // Corrected namespace
{
[Authorize]
public class InventoryController : Controller
{
private readonly AppDbContext _dbContext;
public InventoryController(AppDbContext appDbContext)
{
_dbContext = appDbContext;
}
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Create(InventoryItem model) // Change to InventoryItem
{
if (ModelState.IsValid)
{
// If needed, convert InventoryItem to Product here
var product = new Product
{
ProductName = model.ProductName,
ProductBrand = model.ProductBrand,
Quantity = model.Quantity,
TypeOfProduct = model.TypeOfProduct,
AgeRange = model.AgeRange
};
await _dbContext.Products.AddAsync(product);
await _dbContext.SaveChangesAsync();
ViewBag.Message = "Product created successfully!";
return RedirectToAction("Index"); // Adjust as needed
}
return View(model); // Return the view with validation errors
}
}
}
Код: Выделить всё
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
}
}
Код: Выделить всё
@if (@ViewBag.Message != null)
{
@ViewBag.Message
}
@* *@
@* *@
@* *@
Type of Product
Cloth
Pants
Accessories
Sports Gear
Age
-18
18+
Create Item
Подробнее здесь: https://stackoverflow.com/questions/790 ... trollers-o
Мобильная версия