How is Modelstate.IsValid triggered?

Hello! I am reading this tutorial on how DTO's can be implemented with API's: https://dev.to/moe23/net-6-automapper-data-transfer-objects-dtos-49e 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};
}
DEV Community
.NET 6 - AutoMapper & Data Transfer Objects (DTOs) 🗺
Intro In this article we will be exploring AutoMapper and Data Transfer Objects (DTOs) in...
39 Replies
Anton
Anton2mo ago
If it can't bind json to that object. Like if it can't parse a string as a datetime. Or if the json is invalid in some way too, I think. Like if you pass tru instead of true.
△ electronic heartbreak.
Would not the _mapper function then crash?
Anton
Anton2mo ago
No the parameter will have the defaults for those properties then But that cab only happen if you turn off the filter that shortcircuits this in MVC generally, it would return a response automatically at that point Or maybe it will just be null? not sure the mapper has nothing to do with ModelState
△ electronic heartbreak.
I have a little hard time to understand this. thats correct
Anton
Anton2mo ago
well just try passing some garbage json with the default mvc settings and see what error you get it won't even get to your code
△ electronic heartbreak.
oh like that.
Anton
Anton2mo ago
But it will ignore extra fields, I'm pretty sure and if you don't provide a value for a property, it will initialize it to null
△ electronic heartbreak.
if we check the Driver model for example in the code above. Lets say the driver number should be between 1 and 5 and the DTO contains 0? then it should fail
Anton
Anton2mo ago
no it won't handle that automatically unless you do data annotations and enable them, I think they aren't on by default
△ electronic heartbreak.
yes data annotations should be added. But you should not add data annotations on DTO's
Anton
Anton2mo ago
Well then you won't be using MVC's system for validation which isn't good in the first place, to be fair data annotations are pretty primitive
△ electronic heartbreak.
Since the DTO comes in at the controller. you would 'normally' validate the object that comes in.
Anton
Anton2mo ago
well one thing for certain
△ electronic heartbreak.
i do say that adding basic validation, like required or min/max length would be OK.
Anton
Anton2mo ago
mapping typically never does any validation
Want results from more Discord servers?
Add your server