Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have POST method in CustomerController class inside WEBAPI as
C#
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
C#
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
C#
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);

                               //Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
                           }

                       }

                       @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
C#
@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 .
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900