0

I have used tempData to hold a message returned from database to controller and then using it in view. It worked until I made an ajax call to post data.

[HttpPost]
public ActionResult AddAppointment(AddBookingsViewModel AddBookingVM) {

    BookingsRepository BookingRep = new BookingsRepository();

    if (ModelState.IsValid) {
        int ReturnRowsCount = BookingRep.InsertCustomerAppointments(AddBookingVM, out ReturnStatus, out ReturnMessage, out ReturnBookingID);

        if (ReturnRowsCount > 0){
                TempData["Message"] = ReturnMessage;
        }

        else {
            TempData["Message"] = ReturnMessage;
        }
    }

    else {
        TempData["Message"] = "Some fields are required";
    }

    return View(AddBookingVM);
}

View:

@model ZahidCarWash.ViewModels.AddBookingsViewModel

@{
    ViewBag.Title = "Add Appointment";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="divMessage" class="alert alert-info fade in MessageStrip">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    @TempData["Message"].ToString()
</div>

@if (!string.IsNullOrEmpty(TempData["Message"].ToString())) {
    <script type="text/javascript">$("#divMessage").show();</script>
}

else {
    <script type="text/javascript">$("#divMessage").hide();</script>
}

ajax's call:

$('form').submit(function (e) {
    e.preventDefault();
    if (!$(this).valid()) {
        return;
    }

    var url = '@Url.Action("AddAppointment")';
    var data = $(this).serialize();

    $.post(url, data, function (response) {

    });
});

It was working when I was using Form.Post in begin form but it stopped working when I started using making ajax call.

I set a breakpoint and it reaches there but not displaying.

Why?

Meloman
  • 3,558
  • 3
  • 41
  • 51
  • Any console errors? Does the form action have another origin? Assuming response is actually used somewhere: `$.post(url, data, function (response) { $("#someontainer").html(response); });` – mplungjan Apr 11 '18 at 12:08
  • 1
    Cause you are not using it. `function (response) {}` should actual place response content on the page – Andrei Apr 11 '18 at 12:08
  • at the moment in your code you are making the request, but you do nothing with the response. So possibly it worked on the server-side but you haven't written any code to handle that in your UI. BTW if you are using ajax the server should really return a Partial View (or even just some JSON), not a whole view (which include all the HTML head, body tags etc that are already on your page, you don't need those again, you need only what's necessary for updating _within_ the page) – ADyson Apr 11 '18 at 12:09
  • TempData is useful when you transfer data from action to action. Change it to ViewBag or ViewData if you want it on view – Sandip Apr 11 '18 at 12:13
  • 1
    @Sandip that dont make sense here both temp data and viewbag not gonna work untill the whole view is rerender and ajax wont do that – RAHUL S R Apr 11 '18 at 12:14
  • @Sandip neither of these will be returned as part of a response from ajax. Only final rendered view HTML is returned, _after_ it has been processed by the Razor engine – ADyson Apr 11 '18 at 12:27
  • @ADyson. Agree. It was just attempt made to try once before doing any code changes. – Sandip Apr 11 '18 at 12:36

0 Answers0