i have POST method in CustomerController class inside WEBAPI as
public IHttpActionResult PostNewCustomer(CustomerViewModel customer)
{
if (!ModelState.IsValid)
return BadRequest("Invalid data. Please recheck!");
using(var x=new WebAPIDemo_DBEntities())
{
x.Customers.Add(new Customer()
{
name=customer.Name,
email=customer.Email,
address=customer.Address,
phone=customer.Phone
});
x.SaveChanges();
}
return Ok(customer);
CustomerViewModel is ModelClass
public class CustomerViewModel
{
public int Id { get; set; }
public String Name { get; set; }
public String Email { get; set; }
public String Address { get; set; }
public String Phone { get; set; }
}
Id is Primary Key and autoIncrement
I am using Retrofit in android to process my Resonse/Requests.
From Androd Client Side i am Calling the POST method of web api to Insert the values
try{
CustomersListModel newCustomer= new CustomersListModel();
newCustomer.setName(nameEt.getText().toString());
newCustomer.setPhone(phoneEt.getText().toString());
newCustomer.setEmail(emailEt.getText().toString());
newCustomer.setAddress(addressEt.getText().toString());
Call call = RetrofitClient.getInstance().getMyApi().PostNewCustomer(newCustomer);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if(response.isSuccessful()){
Intent intent = new Intent(AddUpdateRecordActivity.this,MainActivity.class);
startActivity(intent);
}
}
@Override
public void onFailure(Call call, Throwable t) {
Toast.makeText(getApplicationContext(),"failed",Toast.LENGTH_SHORT).show();
}
});
}
catch (Exception e){
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
inside Api.java Interface i have declared
@POST("api/customer")
Call<CustomersListModel> PostNewCustomer(@Body CustomersListModel customersListModel);
What I have tried:
The data is getting inserted Successfully . My problem is I want to fetch the Last inserted ID through WebApi when the Response is Success. how to do this .