The process in this tutorial seems normal and you can easily follow along.
There are only 2 things I have my questions about:
1. The Driver class in this tutorial has no DataAnnotations (see first codeblock). How come the Modelstate.IsValid can be triggered in codeblock 2?
namespace SampleMapper.Models;public class Driver{ public Guid Id { get; set; } public string FirstName { get; set; } = ""; public string LastName { get; set; } = ""; public int DriverNumber { get; set; } public DateTime DateAdded { get; set; } public DateTime DateUpdated { get; set; } public int Status { get; set; } public int WorldChampionships { get; set; }}
namespace SampleMapper.Models;public class Driver{ public Guid Id { get; set; } public string FirstName { get; set; } = ""; public string LastName { get; set; } = ""; public int DriverNumber { get; set; } public DateTime DateAdded { get; set; } public DateTime DateUpdated { get; set; } public int Status { get; set; } public int WorldChampionships { get; set; }}
2. How does Modelstate.IsValid get triggered when a Dto is passed but it gets mapped to an entity?
[HttpPost] public IActionResult CreateDriver(DriverCreationDto data) { var _driver = _mapper.Map<Driver>(data); if(ModelState.IsValid) { drivers.Add(_driver); return CreatedAtAction("GetDriver", new {_driver.Id}, data); } return new JsonResult("Something went wrong") {StatusCode = 500}; }
[HttpPost] public IActionResult CreateDriver(DriverCreationDto data) { var _driver = _mapper.Map<Driver>(data); if(ModelState.IsValid) { drivers.Add(_driver); return CreatedAtAction("GetDriver", new {_driver.Id}, data); } return new JsonResult("Something went wrong") {StatusCode = 500}; }