Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. .NET Core & (auto)binding: Is it a bug?

.NET Core & (auto)binding: Is it a bug?

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpjavascriptasp-netdotnetwpf
6 Posts 4 Posters 45 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • raddevusR Offline
    raddevusR Offline
    raddevus
    wrote on last edited by
    #1

    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 values

    var 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

    0 Richard DeemingR 2 Replies Last reply
    0
    • raddevusR raddevus

      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 values

      var 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

      0 Offline
      0 Offline
      0x01AA
      wrote on last edited by
      #2

      Automatism is often also random. Either you declare what absolutely has to be bound or you have to live with chance.

      raddevusR 1 Reply Last reply
      0
      • 0 0x01AA

        Automatism is often also random. Either you declare what absolutely has to be bound or you have to live with chance.

        raddevusR Offline
        raddevusR Offline
        raddevus
        wrote on last edited by
        #3

        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:

        1 Reply Last reply
        0
        • raddevusR raddevus

          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 values

          var 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

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          raddevusR J 2 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            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

            raddevusR Offline
            raddevusR Offline
            raddevus
            wrote on last edited by
            #5

            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);

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              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

              J Offline
              J Offline
              jochance
              wrote on last edited by
              #6

              Gonna guess this is the "(almost)". Heh.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups