5
Hello,
Put a debugger in this method, Your BookingFrom and bookingTo date not saving the correct way due to all other calculations not working.
check this line :
int numberOfDays = Convert.ToInt32((objBookingViewModel.BookingTo - objBookingViewModel.BookingFrom).TotalDays);
first thing to check this one: objBookingViewModel.BookingTo
what values you are getting in both date fields ?
public ActionResult SaveRoomBooking(BookingViewModel objBookingViewModel)
{
int numberOfDays = Convert.ToInt32((objBookingViewModel.BookingFrom - objBookingViewModel.BookingTo).TotalDays);
Room objRoom = objHotelDbEntities.Rooms.Single(model => model.RoomId == objBookingViewModel.AssignRoomId);
decimal RoomPrice = objRoom.RoomPrice;
decimal TotalAmount = objRoom.RoomPrice * numberOfDays;
RoomBooking roomBooking = new RoomBooking()
{
BookingFrom = objBookingViewModel.BookingFrom,
BookingTo = objBookingViewModel.BookingTo,
AssignRoomId = objBookingViewModel.AssignRoomId,
Address = objBookingViewModel.Address,
CustomerName = objBookingViewModel.CustomerName,
PhoneNo = objBookingViewModel.PhoneNo,
NoOfMembers = objBookingViewModel.AssignRoomId,
TotalAmount = objBookingViewModel.TotalAmount
};
objHotelDbEntities.RoomBookings.Add(roomBooking);
objHotelDbEntities.SaveChanges();
objRoom.BookingStatusId = 3;
objHotelDbEntities.SaveChanges();
return Json(new{message="Hotel Booking is Successfully Created.",success=true}, JsonRequestBehavior.AllowGet);
}
If you make date fields work correctly then all other depends on that.

Accepted 5
Hello Emmmanuel,
You need to pass date field with Text(), please do below change that will work, instead of .val() you need to use .text() for date field
var objBookingViewModel = {};
objBookingViewModel.CustomerName = $("#txtCustomerName").val();
objBookingViewModel.Address = $("#txtCustomerAddress").val();
objBookingViewModel.PhoneNo = $("#txtPhoneNo").val();
objBookingViewModel.BookingFrom = $("#txtBookingFrom").text();
objBookingViewModel.BookingTo = $("#txtBookingTo").text();
objBookingViewModel.AssignRoomId = $("#ddAssignRoom").val();
objBookingViewModel.NoOfMembers = $("#txtNoOfMembers").val();
var data = JSON.stringify({
objBookingViewModel: objBookingViewModel
});
4
It seems like there might be a couple of issues in your code. Let's address them:
-
Date Calculation Issue: In your SaveRoomBooking
action method, you're calculating the number of days incorrectly. Instead of (objBookingViewModel.BookingFrom - objBookingViewModel.BookingTo)
, it should be (objBookingViewModel.BookingTo - objBookingViewModel.BookingFrom)
to get the correct number of days.
-
TotalAmount Calculation Issue: You're calculating the TotalAmount
using objBookingViewModel.TotalAmount
, which seems incorrect. It should be calculated based on the room price and the number of days.
-
NoOfMembers Assignment Issue: You're assigning objBookingViewModel.AssignRoomId
to NoOfMembers
, which seems incorrect. It should be assigned based on the number of members input.
Here's how you can modify your SaveRoomBooking
method:
public ActionResult SaveRoomBooking(BookingViewModel objBookingViewModel)
{
// Calculate the number of days correctly
int numberOfDays = (objBookingViewModel.BookingTo - objBookingViewModel.BookingFrom).Days;
// Fetch the room details
Room objRoom = objHotelDbEntities.Rooms.Single(model => model.RoomId == objBookingViewModel.AssignRoomId);
// Calculate the total amount based on the room price and number of days
decimal TotalAmount = objRoom.RoomPrice * numberOfDays;
// Create RoomBooking object
RoomBooking roomBooking = new RoomBooking()
{
BookingFrom = objBookingViewModel.BookingFrom,
BookingTo = objBookingViewModel.BookingTo,
AssignRoomId = objBookingViewModel.AssignRoomId,
Address = objBookingViewModel.Address,
CustomerName = objBookingViewModel.CustomerName,
PhoneNo = objBookingViewModel.PhoneNo,
NoOfMembers = objBookingViewModel.NoOfMembers, // Corrected assignment
TotalAmount = TotalAmount // Assign the calculated total amount
};
// Add roomBooking to database
objHotelDbEntities.RoomBookings.Add(roomBooking);
objHotelDbEntities.SaveChanges();
// Update booking status
objRoom.BookingStatusId = 3;
objHotelDbEntities.SaveChanges();
return Json(new { message = "Hotel Booking is Successfully Created.", success = true }, JsonRequestBehavior.AllowGet);
}
Make sure to correct these issues and test your code again. If you encounter any further issues, feel free to ask!

3
Thank you team,
I change the format for the date textbox and everything is working now
3
Hello,
In your latest updated code still you are getting your both date fields empty. Did you check your both date input why is it coming empty ?
you need to check these two line properly :
objBookingViewModel.BookingFrom = $("#txtBookingFrom").text();
objBookingViewModel.BookingTo = $("#txtBookingTo").text();
Please do these check for above line,
Step - 1 : objBookingViewModel.BookingFrom what is datatype of this field in your class ?
step - 2 : check by debugging why $("#txtBookingFrom").text() text input you are not getting value ?
3
Ensure that the date format used in the date picker matches the format expected by your BookingViewModel
. You are using the format dd-MMM-yyyy
in your view model ("{0:dd-MMM-yyyy}"
), so make sure the date picker provides dates in this format.
Use browser developer tools to inspect the elements and check if the values are being populated correctly into the date picker inputs. You can also use console.log
statements to log the values fetched by jQuery to debug further.
3
Hello Emmmanuel,
Can you please double check on the javascript code, or in that add debugger ang check what value are you got in the javascript side.
I believe you are not as expected value from the javascript side.
Thanks
3
Could you please attach debugger here and what values you getting in these two fields after click on save button,
public ActionResult SaveRoomBooking(BookingViewModel objBookingViewModel)
{
// Calculate the number of days correctly
int numberOfDays = (objBookingViewModel.BookingTo - objBookingViewModel.BookingFrom).Days;
// value for objBookingViewModel.BookingTo
// value for objBookingViewModel.BookingFrom
}
3
Have you check while SaveRoomBooking method what are the values for BookingTo and BookingFrom fields value?
you are not getting value from your form data.
SaveRoomBooking(BookingViewModel objBookingViewModel)
Could you please share more details for your click button from where you are calling the "SaveRoomBooking" method?
3
The date value is not coming properly from the request that case also happen here.
3
Hello,
Please put the debugger in your action method, amd checked what date value you are getting then you have more idea what the things are happen in your code side.
If possible then share the debugging screenshot so we can check and provide to you appropriate solution as well.
Thanks.
3
Hello Kumar,
I modified the code as you said but the error is still the same