Mark
Mark

Reputation: 7818

ASP.Net MVC Get ID of newly added record from AJAX post

How do I retrieve the ID from the following model, after the following JQuery/Ajax posts to it:

JQuery:

       $.ajax({
            type: 'POST',
            url: '/api/searchapi/Post',
            contentType: 'application/json; charset=utf-8',
            data: toSend,
        }).done(function (msg) {
                alert( "Data Saved: " + msg );
        });

Controller:

    // POST api/searchapi
    public Void Post(Booking booking)
    {
        if (ModelState.IsValid)
        {
            tblCustomerBooking cust = new tblCustomerBooking();
            cust.customer_email = booking.Email;
            cust.customer_name = booking.Name;
            cust.customer_tel = booking.Tel;

            bc.tblCustomerBookings.Add(cust);
            bc.SaveChanges();

            long ID = cust.customer_id;

            Return ID;   <-- what do I enter here?
         }

         Return "Error";  <-- and here?
     }  

How can I get the ID back into the jQuery script, and if the model isn't valid, how do I return an error to the jQuery?

Thanks for any help,

Mark

Upvotes: 0

Views: 3171

Answers (4)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

You could return a JsonResult

[HttpPost]
public ActionResult Post(Booking booking)
{
    if (ModelState.IsValid)
    {
        tblCustomerBooking cust = new tblCustomerBooking();
        cust.customer_email = booking.Email;
        cust.customer_name = booking.Name;
        cust.customer_tel = booking.Tel;

        bc.tblCustomerBookings.Add(cust);
        bc.SaveChanges();

        return Json(new { id = cust.customer_id });
     }

     return HttpNotFound();
 }  

and then on the client simply:

$.ajax({
    type: 'POST',
    url: '/api/searchapi/Post',
    contentType: 'application/json; charset=utf-8',
    data: toSend,
}).done(function (msg) {
    alert('Customer id: ' + msg.id);
}).error(function(){
    // do something if the request failed
});

Upvotes: 3

von v.
von v.

Reputation: 17108

One way to do it is like this:

In your controller:

 public Void Post(Booking booking)
    {

         //if valid
         return Json(new {Success = true, Id = 5}); //5 as an example

         // if there's an error
         return Json(new {Success = false, Message = "your error message"}); //5 as an example
     }  

In your ajax post:

$.ajax({
        type: 'POST',
        url: '/api/searchapi/Post',
        contentType: 'application/json; charset=utf-8',
        data: toSend,
        success: function(result) {
          if (result.Success) {
            alert(result.Id);
          }
          else {
            alert(result.Message);
          }
        }
});

Upvotes: 3

user2224780
user2224780

Reputation: 71

returning void isn't a good idea, use JsonResult

Because you are using JavaScript, I recommend using JSON.

return this.Json(new { customerId = cust.customer_id});

Upvotes: 1

Rick Hodder
Rick Hodder

Reputation: 2252

In order to receive a return value, the controller method must return something other than void.

Does your controller compile? Your post method has a void return type, so it should fail compilation when it sees the return ID and return "Error" commands

Upvotes: 0

Related Questions