.NET Core 8.x and Auto Binding: Is This a Bug?

Challenges In Software Development Career

My career seems to be entirely described by the following:

“Use a simple technology to do a thing that 80% of Devs take for granted and who also tell you it works, but discovering that the thing doesn’t actually work the way they say it works.”

Here’s the latest. I will try to keep it short (because I’m going to write an article on it, but I can’t wait to share this one because it is so weird (from multiple angles)).

 JavaScript FormData Posted To Backend Is All Strings

Mabye a lot of you know this, but all of the values in the following (real) example end up being strings when they get to the backend.

//first notice that I'm appending two items to the formData.
// 1. string uuid
// 2. journalEntry object with numerous values
var formData = new FormData();
var entryId = 9;
var jentry = {
Title: titleText,
Note: noteText,
Created: createdDate,
Updated: null
// You have to do this weird thing to add the data to FormData.
for (var key in jentry) {
formData.append(key, jentry[key]);

Odd, But Acceptable

Ok, so the point of that is that all of the values in the jentry object are converted to string values. That means Updated = "null" (string) and the Id = "9".

This is odd, to me, but acceptable.
If you refute this, please provide citations. I’ve searched far & wide and asked Copilot — all says they are strings when they hit the backend.

Now It Gets Real Weird

My WebAPI method which gets called by the JS Fetch with the previously shown FormData looks like:

public ActionResult Save([FromForm] String uuid,[FromForm] JournalEntry jentry)

AutoBinding In .NET Core Works With JS Fetch

1. The JS Fetch works.
2. The C# JournalEntry is created from the data posted via the FormData variables.
3. I can examine the data in the C# JournalEntry object and (almost) everything looks fine.

Autobinding Ignores The Id Value!

However, at some point I needed the Id value which was being passed in.
But I noticed that the Id value in the C# object was always ZERO Id = 0.
FormData Value Is Non-Zero, But Autobound Object Is 0
But, keep in mind that the FormData object shown above is passing in a non-zero value for Id (9 in the example.


Why Is This Happening?

The only thing I could guess is that since the Id value (from the FormData) was being passed as a String that the autobinding just ignores the value and sets it to 0.

Here’s How My C# Object (Used for Autobinding) Is Defined

public class JournalEntry{
public Int64 Id{get;set;}
public String? Title{get;set;}
public String Note{get;set;}
public String Created{get;set;}
public String? Updated{get;set;}

Yes, the point is that the Id is an Int64 (not a string).

This Part Should Alarm You!!! This Should Be The Bug
So, somehow the autobinder is able to bind the JournalEntry to the FormData object but it just ignores the Id value and sets it to 0!!!!

Why?? !! Please someone explain it to me.

A “Workaround” Test

I altered my JournalEntry class so the Id is a String
public class JournalEntry{
public String Id{get;set;}
// ....snip....

and posted the data again.

Autobinder Works! What?!

After that change, the autobinder sets the Id value properly (non-zero value that is read from FormData).

Not What I Expected

This is not what I expected. JSON data is _supposed_ to get autobound to the C# object, right?

Just Binds the Parts It Wants To Bind?

It’s weird because it doesn’t fail to bind it just skips parts of the object that it wants to skip.
I could accept it if it failed to bind because it was the wrong type.

This is really weird.¬† I found a workaround but it took a lot of work.¬† I’ll post that with samples soon.

Leave a Reply

Your email address will not be published. Required fields are marked *