title: How to send post data from React frontend to .NET backend
body: <p>I have a webpage where a user signs up to become a user by filling out four textboxes and clicking submit. When they click submit, an api call is fired and sends that data to my controller. It then takes that data and maps it to the columns from my model, where it is then added to the DbSet. I am getting the error <code>Cannot insert the value NULL into column 'email', table 'IssueTrackerDB.dbo.Users'; column does not allow nulls. INSERT fails.</code> Keep in mind I am entering values for all four textboxes when I test it, so email isn't getting left out. Also, do I have to provide names or ids for each textbox in the frontend, so it maps to my model successfully?</p>
  <p><strong>Value of Users object after it is created</strong>
  <a href="https://i.stack.imgur.com/DBpsl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DBpsl.png" alt="Value of Users object after it is created" /></a></p>
  <p><strong>React Frontend</strong></p>
  <pre><code>import React, { Component } from &quot;react&quot;;
  import { useState } from &quot;react&quot;;
  import { Grid, TextField, Button, Typography } from &quot;@material-ui/core&quot;;
  
  const Signup = () =&gt; {
      const [username, setUsername] = useState();
      const [password, setPassword] = useState();
      const [email, setEmail] = useState();
      const [role, setRole] = useState()
  
      const post = () =&gt; {
          const requestOptions = {
              method: &quot;POST&quot;,
              headers: { &quot;Content-Type&quot;: &quot;application/json&quot; },
              body: JSON.stringify({
                  username: username,
                  password: password,
                  email: email,
                  role: role
              }),
              
          };
          fetch(&quot;https://localhost:5001/api/IssueTracker&quot;, requestOptions)
              .then((response) =&gt; response.text())
              .then((data) =&gt; {
                 
  
              })
      }
  
      return (
          &lt;div&gt;
              &lt;body&gt;
                  &lt;form action=&quot;#&quot; method=&quot;POST&quot;&gt;
                      &lt;TextField onChange={(e) =&gt; setUsername(e.target.value)}&gt; &lt;/TextField&gt;
                      &lt;br&gt;
                      &lt;/br&gt;
                      &lt;TextField onChange={(e) =&gt; setPassword(e.target.value)}&gt; &lt;/TextField&gt;
                      &lt;br&gt;
                      &lt;/br&gt;
                      &lt;TextField onChange={(e) =&gt; setEmail(e.target.value)}&gt; &lt;/TextField&gt;
                      &lt;br&gt;
                      &lt;/br&gt;
                      &lt;TextField onChange={(e) =&gt; setRole(e.target.value)}&gt; &lt;/TextField&gt;
                      &lt;br&gt;
                      &lt;/br&gt;
                      &lt;Button onClick={() =&gt; post()}&gt; Sign Up&lt;/Button&gt;
                  &lt;/form&gt;
              &lt;/body&gt;
          &lt;/div&gt;
      );
  }
  
  
  
  export default Signup;
  </code></pre>
  <p><strong>Controller</strong></p>
  <pre><code>// POST api/values
          [HttpPost]
          public void Post(Users user)
          {
              _repository.CreateUser(user);
              _repository.SaveChanges();
          }
  </code></pre>
  <p><strong>Model</strong></p>
  <pre><code>namespace IssueTracker.Models
  {
      public class Users
      {
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public string id { get; set; }
  
          [Required]
          public string username { get; set; }
  
          [Required]
          public string password { get; set; }
  
          [Required]
          public string email { get; set; }
  
          [Required]
          public string role { get; set; }
  
          public Users(string username, string password, string email, string role)
          {
              this.username = username;
              this.password = password;
              this.email = email;
              this.role = role;
          }
  
          public Users()
          {
  
          }
  
      }
  }
  </code></pre>
  <p><strong>Interface</strong></p>
  <pre><code>using IssueTracker.Models;
  
  namespace IssueTracker.Data
  {
      public interface IIssueTrackerRepo
      {
          bool SaveChanges();
          //IEnumerable&lt;Command&gt; GetAllCommands();
          //Command GetCommandById(int id);
          void CreateUser(Users cmd);
          //void UpdateCommand(Command cmd);
          //void DeleteCommand(Command cmd);
      }
  }
  </code></pre>
  <p><strong>Interface Implementation</strong></p>
  <pre><code>using System;
  using IssueTracker.Models;
  namespace IssueTracker.Data
  {
      public class SqlUsersRepo: IIssueTrackerRepo
      {
          private readonly UsersContext _context;
  
          public SqlUsersRepo(UsersContext context)
          {
              _context = context;
          }
  
          public void CreateUser(Users user)
          {
              if (user == null)
              {
                  throw new ArgumentNullException(nameof(user));
              }
              
              _context.Users.Add(user);
          }
  
          public bool SaveChanges()
          {
              return (_context.SaveChanges() &gt;= 0);
          }
      }
  }
  </code></pre>
  <p><strong>UsersContext Class</strong></p>
  <pre><code>using IssueTracker.Models;
  using Microsoft.EntityFrameworkCore;
  namespace IssueTracker.Data
  {
      public class UsersContext : DbContext
      {
          public UsersContext()
          {
          }
  
          public UsersContext(DbContextOptions&lt;UsersContext&gt; opt) : base(opt)
          {
          }
  
          public DbSet&lt;Users&gt; Users { get; set; }
      }
  
  }
  </code></pre>