In this recipe, you will learn what a Partial
view is, and how to create a strongly typed one.
We will create an empty web application with ASP.NET Core MVC enabled, adding the MVC dependency into the project:
"Microsoft.AspNetCore.Mvc": "2.0.0"
We can get the benefits of C# compiler with strongly typed Partial views. C# compiler checks names and types of properties. If it detects misuse (such as wrong property names, and so on), it will throw an exception and won't compile the project:
- First, let's create the
Dto
andViewModel
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; } }
- Next...