Hi I am trying to get the address values from a simple post request to an API so I can read them an populate a SQL table. Items such as name etc I am able to read and write to the database, but each person has 2 addresses so these are in the json as an array (without square brackets). Is there an easy way to do this?
What I have tried:
My model looks like this
public class Customer
{
public int customer_number { get; set; }
public string prefix { get; set; }
public string firstname { get; set; }
public string surname { get; set; }
public string type { get; set; }
public string line_1 { get; set; }
public string line_2 { get; set; }
public string line_3 { get; set; }
public string city { get; set; }
public string county { get; set; }
public string post_code { get; set; }
public string country { get; set; }
public char email_opt_in { get; set; }
}
public class CreateCustomer : Customer
{
}
and my controller like this.
public HttpResponseMessage Post([FromBody]CreateCustomer value)
{
_conCust = new SqlConnection(ConfigurationManager.ConnectionStrings["CAR_Connection"].ConnectionString);
_conCust.Open();
var a_query = "insert into cust ( Title, FirstName, Surname, Address1, Address2, Address3, Town, County, Country, " +
"PostCode, Tel, Mobile, ACCreateDate, CreatedBy, CreatedDate, CreatedMachine) values " +
"(@Title, @First, @Surname, @Add1, @Add2, @Add3, @Town, @County, @Country, @PostCode, @Tel, @Mobile, CONVERT(VARCHAR(10), getdate(), 111), " +
"'WebAPI', getdate(), 'AUTOMATED') ";
SqlCommand a_insertcommand = new SqlCommand(a_query, _conCust);
a_insertcommand.Parameters.AddWithValue("@Title", value.prefix);
a_insertcommand.Parameters.AddWithValue("@First", value.firstname);
a_insertcommand.Parameters.AddWithValue("@Surname", value.surname);
a_insertcommand.Parameters.AddWithValue("@Add1", "");
a_insertcommand.Parameters.AddWithValue("@Add2", "");
a_insertcommand.Parameters.AddWithValue("@Add3", "");
a_insertcommand.Parameters.AddWithValue("@Town", "");
a_insertcommand.Parameters.AddWithValue("@County", "");
a_insertcommand.Parameters.AddWithValue("@Country", "");
a_insertcommand.Parameters.AddWithValue("@PostCode", value.post_code[1]);
a_insertcommand.Parameters.AddWithValue("@Tel", "");
a_insertcommand.Parameters.AddWithValue("@Mobile", "");
int a_result = a_insertcommand.ExecuteNonQuery();
At the moment to test the name saved correctly I've commented out the address values, inserting just a blank string instead.
I know I must be just missing something simple!
A post request would be similar to this...
{
"customer_number": 1,
"prefix": "Mr",
"firstname": "Test",
"surname": "User",
"billing_address": {
"type": "Billing",
"line_1": "Street 1",
"line_2": "Street 2",
"line_3": "Street 3",
"city": "AnyTown",
"county": "Cumbria",
"post_code": "CA1 4TY",
"country": "GB",
"is_default": "1"
},
"shipping_address": {
"type": "Shipping",
"line_1": "Street 1",
"line_2": "Street 2",
"line_3": "Street 3",
"city": "Town",
"county": "Cumbria",
"post_code": "CA1 4TY",
"country": "GB",
"is_default": "0"
},
"email_opt_in": "0|1"
}
Any guidance would be appreciated.