Reputation: 1323
I have a Web API POST Request that should add a new record to a specific table using the attributes found within the body as JSON. However, when entering all the attributes (which I assume to be correct), the request will end up adding a new record, however ALL the attributes will be set to NULL for whatever reason?
Below is my API Post Request:
/*Define URL endpoint for adding a single role*/
[System.Web.Http.Route("api/Roles/addRole")]
/*HttpPost header, ensures only the usage of POST requests*/
[System.Web.Mvc.HttpPost]
/*Main return new list with added role*/
public List<dynamic> addRole([FromBody] Roles roles)
{
if (roles != null)
{
/*Create an instance of the UltimateDB*/
UltimateDBEntities db = new UltimateDBEntities();
/*Optimize memory usage by disabling proxy creation*/
db.Configuration.ProxyCreationEnabled = false;
db.Roles.Add(roles);
db.SaveChanges();
return getAllRoles();
}
else
{
return null;
}
}
The above should add a single record to the "Roles" table, using the data from the body (JSON) entered by the user. I have tested this using Postman, and all that is returned is a new record with all of its attributes set to NULL.
For example, I will enter this:
{
"Name": "Lab Employee",
"Description": "User only has read rights",
"Tenant_rental": 1,
"Property_Module": 0,
"Employee_Management": 0,
"Reports": 1,
"administration": 1,
"user_Password_access": 0,
"Building_Management": 0,
"Credit_Bureau": 1
}
And get this as my new record:
{
"ID": 23,
"Name": null,
"Description": null,
"Tenant_rental": null,
"Property_Module": null,
"Employee_Management": null,
"Reports": null,
"administration": null,
"user_Password_access": null,
"Building_Management": null,
"Credit_Bureau": null
}
My Roles Class:
namespace U18043039_HW1.Models
{
using System;
using System.Collections.Generic;
public partial class Roles
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Roles()
{
this.User_Table = new HashSet<User_Table>();
}
public int Role_ID { get; set; }
public string Role_Name { get; set; }
public string Role_Description { get; set; }
public Nullable<bool> Role_Tenant_rental { get; set; }
public Nullable<bool> Role_Property_Module { get; set; }
public Nullable<bool> Role_Employee_Management { get; set; }
public Nullable<bool> Role_Reports { get; set; }
public Nullable<bool> Role_administration { get; set; }
public Nullable<bool> Role_user_Password_access { get; set; }
public Nullable<bool> Role_Building_Management { get; set; }
public Nullable<bool> Role_Credit_Bureau { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<User_Table> User_Table { get; set; }
}
}
Upvotes: 2
Views: 123
Reputation: 43959
Use this data for test:
{
"Role_Name": "Lab Employee",
"Role_Description": "User only has read rights",
"Role_Tenant_rental": True,
"Role_Property_Module":False,
... and so on
}
Upvotes: 1