In this recipe, you will learn how to create our own HTMLHelpers. The following code is based on the previous recipe, Using dependency injection in views.
We will create an empty web application with ASP.NET Core MVC enabled by adding the MVC dependency into the project:
"Microsoft.AspNetCore.Mvc": "2.0.0".
Now we can dive deep into dependency injection in views:
- First, we add an
AddProduct
action method with the corresponding view and associated repository to this template web project. In this example, the Product list and the Category list come from hardcoded code, but in the real world, it should come from a database (relational or not), a service, or any other data source:- Following code is the
Dto
andViewModel
classes:
- Following code is the
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)]...