- First, let's create the Dto and the ViewModel objects:
public class ProductDto{ public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; }}public class ProductViewModel{ public int Id { get; set; } [Required] [MaxLength(50)] public string Name { get; set; } [Required] [Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive number")] public decimal Price { get; set; } [Required] public string CategoryName { get; set; }}
- Next, we create the service layer used by the controller to retrieve the ProductViewModel. The product controller will use this repository. This repository uses a static, hardcoded data store to make the data persistent across HTTP requests:
public class ...