How do call a controller by ajax i'm certain i have it right in my javascript my url looks like this --> url:
'<%: Url.Action("Contact", "HomeController") %>',
But when im debuging its not calling the controller can anyone help heres my full source,
if (error == false) {
$.ajax({
type: "POST",
url: '<%: Url.Action("Contact", "HomeController") %>',
data: data_string,
timeout: 6000,
error: function(request,error) {
if (error == "timeout") {
$('#contact-error').slideDown('slow');
$('#contact-error span').text('Timed out when contacting server.');
setTimeout(function() {
$('#contact-error').slideUp('slow');
}, 10000);
}
else {
$('#contact-error').slideDown('slow');
$('#contact-error span').text('Something is not working. Please try again.');
setTimeout(function() {
$('#contact-error').slideUp('slow');
}, 10000);
}
},
success: function() {
$('#contact-success').slideDown('slow');
$('#contact-success span').text('Message sent.');
setTimeout(function() {
$('#contact-success').slideUp('slow');
}, 10000);
$('#name').val('');
$('#contactNumber').val('');
$('#message').val('');
}
});
} else {
$('#contact-error').hide();
$('#contact-success').hide();
}
And here is my controller
[HttpPost]
public ActionResult Contact(string name, string contactNumber, string message)
{
try
{
var appSettings = new AppSettingsReader();
var emailAddress = appSettings.GetValue("ToEmailAddress", typeof(string)).ToString();
var mail = new MailMessage { Subject = "Digitali" };
mail.To.Add(emailAddress);
mail.Body = string.Format("Name: {0} <br/> Contact number: {1} <br/><br/> Message: {2} ", name, contactNumber, message);
var mailClient = new SmtpClient();
mailClient.Send(mail);
}
catch (Exception exception)
{
}
return View();
}