Search code examples
asp.net-mvcweb-servicesasp.net-ajaxwebmethod

Making an Asynchronous WebService Call From ASP.Net MVC


I would (as the question states) like to make an asynchronous call, preferably using ASP.net AJAX.

The code for the WebMethod looks like this:

[WebMethod]
public void SendMail(string name, string email, string subject, string body)
{
  MailMessage toSend = new MailMessage(email, address@domain.com, subject, body);
  var smtp = new SmtpClient();
  smtp.Send(toSend);
}

The fields on the view are, not surprisingly: name, email, subject, body.

In my attempts to do this I haven't been able to get to the WebMethod. The service reference is in place, so at least I didn't screw that up.

Thanks for the help...


Solution

  • Here you can find an example of invoking asynchronous methods with AJAX in ASP.NET MVC with elements like

    <% using (Ajax.Form("SendMail", new AjaxOptions { UpdateTargetId = "resultDiv" })) { %>
    
       <!-- Your form elements here... -->
    
    <% } %>
    

    You can receive the params in the controller method and call the webservice from there.