2

Here is my WebAPI POST method which expects BookingDetail and BookingVenue objects:

 [HttpPost] [ValidateUserSession] public JsonResult CheckBooking(BookingDetail BookingDetail, BookingVenue objBV) { try { if (BookingDetail != null && objBV != null) { bool result = Ibook.checkBookingAvailability(BookingDetail, objBV); if (result == false) { return Json("NotAvailable"); } else { return Json("Available"); } } else { return Json("Available"); } } 

}

Angular code from where I'm getting the values from UI and making a post passing these 2 objects:

this.checkbookingavailability = function (Book) { var BookingVenueObj = { EventTypeID: Book.EventSelected, VenueID: Book.Venueselected, GuestCount: Book.NoofGuest, }; var BookingDetailObj = { BookingDate: Book.BookingDate }; var response = $http({ method: "POST", url: "/Booking/CheckBooking/", headers: { 'RequestVerificationToken': $cookies.get('EventChannel') }, data: { BookingDetail: BookingDetailObj, BookingVenue: BookingVenueObj } }); return response; } 

Problem is in my WebAPI code, both the objects as null

5
  • You can not pass two object to web api. You can do like this public JsonResult CheckBooking([frombody]dynamic value) Commented Aug 14, 2016 at 9:32
  • @Div: we can pass two objects in a web api. Commented Aug 14, 2016 at 9:34
  • @ismail baig see this Commented Aug 14, 2016 at 9:39
  • @Div: the link seems to be the question for passing some data from url and other from body which is obvious is not the correct way. but here the question is to pass the object from body (typical POST) Commented Aug 14, 2016 at 9:47
  • You can also use JObject for this - stackoverflow.com/questions/32731486/… Commented Aug 14, 2016 at 16:10

3 Answers 3

2

You can only pass one object in the body so I would recommend you to create a new DTO "BookingDto" for that containing BookingDetail and BookingVenue as member and change the signature of your WebAPI to this:

[HttpPost] [ValidateUserSession] public JsonResult CheckBooking([FromBody]BookingDto bookingObj) 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to serialize JSON object which you are sending to server just by calling JSON.stringify over object.

var response = $http({ method: "POST", url: "/Booking/CheckBooking/", headers: { 'RequestVerificationToken': $cookies.get('EventChannel') }, data: JSON.stringify({ BookingDetail: BookingDetailObj, BookingVenue: BookingVenueObj }) }); return response; 

Comments

0

As dear Pankaj mentioned you need to Serialize your data objects with Stringify function in javascript, Also consider that you must mention that this http request contain Application/JSON content. All these can be shown here:

var response = $http({ method: "POST", url: "/Booking/CheckBooking/", headers: { 'RequestVerificationToken': $cookies.get('EventChannel'), 'Content-Type' : 'application/json' }, data: JSON.stringify({ BookingDetail: BookingDetailObj, BookingVenue: BookingVenueObj }) }); return response; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.