'Empty string not binding to Nullable Guid
So, I have a model which has a Nullable<Guid> property.
public Guid? SupplierID { get; set; }
The raw body has it coming through as
"SupplierID":"". But the application isn't converting it to null, and instead throws a formatting exception.
You can see in the above screenshot that the model is a little more complicated.
This is my controller end point:
[HttpPost]
public async Task<ActionResult> Update([FromBody]TemplateJSONViewModel viewModel)
Things I've tried:
If I remove the [FromBody] attribute, it doesn't bind from the body at all - even though it's a complex type and my understand was FromBody is the default - so perhaps that's a clue?
I've changed the model binding to be a string instead, and it binds fine - coming through as "".
What can cause this? I've not had any issues with "" binding to Null before.
Many thanks for any help, and taking the time to read!
Solution 1:[1]
The Json value is not a supported Guid format.
As the error message in the picture shows,
The GUID is formatted for a display with separators. For example: "{CD9FE4B6-AE1D-448c-B157-D9EA074726CF}"
You bind "" to SupplierID, this ""is a string format, and Guid format is also string , but the format of "" is not the Guid format, Guid format is like "CD9FE4B6-AE1D-448c-B157-D9EA074726CF". You also can set null to the SupplierID if you don't want give value to SupplierID. But you just cannot set Guid format to "".These are two different strings.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Qing Guo |


