.NET Core & (auto)binding: Is it a bug?
-
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)). 1) 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 valuesvar formData = new FormData();
formData.append("uuid",currentUuid);var entryId = 9;
var jentry = {
Id:entryId,
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. What!?! Why Is This Happening? The only thing I could guess is that since the Id value (from the FormData) was b
-
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)). 1) 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 valuesvar formData = new FormData();
formData.append("uuid",currentUuid);var entryId = 9;
var jentry = {
Id:entryId,
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. What!?! Why Is This Happening? The only thing I could guess is that since the Id value (from the FormData) was b
-
Automatism is often also random. Either you declare what absolutely has to be bound or you have to live with chance.
0x01AA wrote:
Automatism is often also random.
That's quite true. We often interact with behavior that is basically _undefined_. Undefined behavior could result in anything. So, I guess software development is just a form of "trial and error". Just see what you get. This is why it is illegal in many areas to call it software engineering. :rolleyes:
-
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)). 1) 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 valuesvar formData = new FormData();
formData.append("uuid",currentUuid);var entryId = 9;
var jentry = {
Id:entryId,
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. What!?! Why Is This Happening? The only thing I could guess is that since the Id value (from the FormData) was b
Quote:
formData.append(uuid,{"uuid":currentUuid});
I'm assuming that's a typo in your question. If not, you're sending a value with the name set to whatever's in your
uuid
variable, and the value set to the literal string"[object Object]"
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Quote:
formData.append(uuid,{"uuid":currentUuid});
I'm assuming that's a typo in your question. If not, you're sending a value with the name set to whatever's in your
uuid
variable, and the value set to the literal string"[object Object]"
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks for pointing that out. :thumbsup: That was a typo / bad test from one of the numerous examples I was trying to get it to work. I updated my original post to fix it to the correct value for uuid I was sending through:
formData.append("uuid",currentUuid);
-
Quote:
formData.append(uuid,{"uuid":currentUuid});
I'm assuming that's a typo in your question. If not, you're sending a value with the name set to whatever's in your
uuid
variable, and the value set to the literal string"[object Object]"
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer