Change the form call header (add a parameter to the done
function):
@using (Ajax.BeginForm("AttendeeAvailability", "Response",
new AjaxOptions { HttpMethod = "POST", OnSuccess = "done(data)" }))
...and the JavaScript:
<script type="text/javascript">
function done(data) {
//do something with the data
}
</script>
How your data
looks depends entirely on how you structure it in your action:
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult AttendeeAvailability(AttendeeAvailableDateTime response)
{
....
....
if (somecheck1)
{
//ModelState.AddModelError(string.Empty,"Check error1");
return Json(new { error = "Check error1" }, JsonRequestBehavior.AllowGet);
}
if (doesTimeExists2)
{
//ModelState.AddModelError(string.Empty,"Check error2");
return Json(new { error = "Check error2" }, JsonRequestBehavior.AllowGet);
}
//do some database actions
return Json(new { success = "Success" }, JsonRequestBehavior.AllowGet);
}
Following this, you can access the data by parsing it:
<script type="text/javascript">
function done(data) {
var confirmation = data;
if (confirmation["success"] != undefined) {
alert(confirmation["success"]);
}
else if (confirmation["error"] != undefined) {
alert(confirmation["error"]);
}
}
</script>