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">×</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?