12
Answers

Booking date calculated wrongly

Hello Team,

In my project, I have the Room page and booking page, so at the booking page when I select the bookingFrom to bookingTo date, and the Room number I expected the room amount to multiple by the number of days book, but things are not working that way, kindly check it for me.

Database

Home Controller

public ActionResult RoomBooking()
{
    var objBookingViewModel = new BookingViewModel();
    var room = objHotelDbEntities.Rooms.ToList();
    objBookingViewModel.ListOfRoom = room.Select(obj => new SelectListItem

    {
        Text = obj.RoomNumber,
        Value = obj.RoomId.ToString(),
        Selected = false
    });
    objBookingViewModel.BookingFrom = DateTime.Now;
    objBookingViewModel.BookingTo = DateTime.Now.AddDays(1);
    return View(objBookingViewModel);
}


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);
}
C#

Answers (12)

5
Photo of Jignesh Kumar
30 39.6k 2.9m 1y

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
Photo of Jignesh Kumar
30 39.6k 2.9m 1y

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
Photo of Prasad Raveendran
228 8.3k 1.9m 1y

It seems like there might be a couple of issues in your code. Let's address them:

  1. 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.

  2. 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.

  3. 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
Photo of Emmmanuel FIADUFE
631 1.4k 76.7k 1y

Thank you team,

I change the format for the date textbox and everything is working now

3
Photo of Jignesh Kumar
30 39.6k 2.9m 1y

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
Photo of Prasad Raveendran
228 8.3k 1.9m 1y

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
Photo of Jaimin Shethiya
49 30.7k 603.5k 1y

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
Photo of Jignesh Kumar
30 39.6k 2.9m 1y

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
Photo of Jignesh Kumar
30 39.6k 2.9m 1y

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
Photo of Jaimin Shethiya
49 30.7k 603.5k 1y

The date value is not coming properly from the request that case also happen here.

3
Photo of Jaimin Shethiya
49 30.7k 603.5k 1y

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
Photo of Emmmanuel FIADUFE
631 1.4k 76.7k 1y

Hello Kumar,

I modified the code as you said but the error is still the same