Question

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...

Was it helpful?

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.

OTHER TIPS

This is not an answer to your question but a warning. I was looking at this method and thinking, "hmm, I wonder if ASP.NET cares if a call to this web method comes from your site or somewhere else?" A quick google search leads me to believe that there isn't any checking to make sure some douche isn't using your web methods for his own malicious desires (here's a blog post talking about this).

So, before you get this working, you might want to think about ways to prevent someone from hijacking your webmethod to send me Viagra emails. Because if I get a Viagra email from your website, I'm not going to be very happy with you.

According to the MSDN Library

In order for a Web service to be accessed from script, it must be an .asmx Web service whose Web service class is qualified with the ScriptServiceAttribute attribute. Individual methods to be called from script must be qualified with the WebMethodAttribute attribute.

see http://msdn.microsoft.com/en-ca/library/bb398998.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top