Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do call a controller by ajax i'm certain i have it right in my javascript my url looks like this --> url:
JavaScript
'<%: Url.Action("Contact", "HomeController") %>',


But when im debuging its not calling the controller can anyone help heres my full source,

C#
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

C#
[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();
        }
Posted

try

url: '<%: Url.Action("Contact", "Home") %>'

or

url : '/Home/Contact'
 
Share this answer
 
v2
Comments
Nico_Travassos 9-Oct-14 4:20am    
Both don't work :( Does not even hit the breakpoint i set in contoller.
nrgjack 9-Oct-14 4:40am    
do you have areas ?
you made changes to the route config ?
Nico_Travassos 9-Oct-14 4:41am    
I made no change to route config.
nrgjack 9-Oct-14 4:44am    
try adding
error: function(request,error) {
console.log({r:request,e:error});
..

then you can log the error and see what happen
do you have some sort of authentication in your project ?

Nico_Travassos 9-Oct-14 5:08am    
Did that im getting this error --> Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:53871/HomeContact
try
var myUrl = "~/Home/Contact";
$.ajax({
type:"GET/POST",
url: myUrl,
success:function(){

} 
});


Try this if this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900