I have the following ajax call :
$.ajax({
url: "@Url.Action("GetMatch", "Test")",
data: {
Name: $("[name='Name']").val(),
Surname: $("[name='Surname']").val(),
Email: $("[name='Email']").val(),
},
dataType: "text",
type: "POST",
success: function (data, textStatus, jqXHR) {
alert(data.length);
if (data.length <= 13){
SaveQuote();
} else {
alert("matche")
}
});
The GetMatch action in the controller is as below:
[HttpPost]
public ActionResult GetClientMatch(ClientBusinessModel contactPerson)
{
*search for a list*
if (list.Count > 0)
{
return PartialView("~/Views/Shared/Partials/_Client.cshtml", list);
} else
return Json(new { data = contactPerson }, JsonRequestBehavior.AllowGet);
}
What I want to do is upon success, if the list is not empty, display the partial view _Client.cshtml. This is ok. But in case the the list is empty, call another action of the controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateClient(SchoolModel school, ClientBusinessModel contactPerson, long QuotationId, long LeadId)
{
*some aciotns*
}
The first thing is how do I test if the data returned, upon the ajax call to action GetClientMatch, is a partial View or the contactPerson? Right now, I am doing the test on the length of the data. And also how do I save the form data AND the contactPerson returned by the first ajax call?
My SaveQuote function:
function SaveQuote() {
var obj = $('#frmSchool').serialize();
var jsonObj = @Html.Raw(Json.Encode(Model));
$.ajax({
type: "POST",
url: "@Url.Action("CreateClient", "Test")",
data: obj + "
Name: $("[name='Name']").val(),
Surname: $("[name='Surname']").val(),
Email: $("[name='Email']").val(),
success: function (data) {
$('#ProductInformation').html(data);
},
error: function (xhr, status, error) {
alert(error);
}
});
}
How can I hit the controller action with the form data and the contactPerson model when the list returned in the first ajax call is empty?