views:

398

answers:

4

I have an asp.net mvc application and i need to to use ajax in this application , i need to add a scriptmanager and add a service refrences with the path of a web service and then call the web service from tag . I have the following code and it doesn't see the web service :

<form>
<input id="SubmitBtn" type="button" value="Sumbit" onclick="TestService(); return false;" />
   <asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/MVCService.asmx" />
    </Services>
    </asp:ScriptManager> 
<script type="text/javascript" language="javascript">  
    function TestService()
    {
        alert('Welcome');
        var ret = MVCService.HelloWorld(OnCheckComplete,OnFailed,OnTimeOut);
    }
    function OnCheckComplete(arg)
    {
         alert(arg);
    }
    function OnFailed(arg)
    {
        alert(arg);
    }
    function OnTimeOut(arg)
    {
        alert(arg);
    }
     </script>
</form>

And it reports an error say : MVCService is undefined so how can i solve this problem ? do i need specific configuration in the MVC application to solve this problem ? I have add ajax toolkit dll to my application refrences and the problem still exist

Thanks in advance

+1  A: 

Just putting the jQuery solution out there if you want to switch to jQuery:

function TestService()
{
    alert('Welcome');

    $.ajax({
        url: "MVCService.asmx/HelloWorld",
        data: "{}",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        processData: false,
        dataType: "json"
        success: function(msg) {
            alert(msg.d);
        }
    });
}

Take sure you have

using System.Web.Script.Services;
    [ScriptService]
public class MVCService : WebService
{
    [WebMethod]
    [ScriptMethod]
    public DateTime HelloWorld()
    {
        return DateTime.Now;
    }
Martin
where to put this jquery code ? in which place in my code i have wrote the full code of the view so can u tell me where to pu this code in order to call web service?
Ahmy
A: 

Or - you can use a regular controller action for this.

If

"foo/bar"

route is configured to

controller="foo", action="bar"

then

$.get("foo/bar", data, callback, "html")

with jQuery will call it.

Arnis L.
A: 

Are you trying to add controls with viewsate in a MVC.NET page?

ScriptManager needs to be in a form runat="server" i think.

Gregoire
I am putting the form tag with runat="server" and not working
Ahmy
+1  A: 

Thanks every one i have found the soltuion for the problem at the folloiwng link : Ajax with asp.net mvc

The following part solved my problem:

<script type="text/javascript" src="../../Content/MicrosoftAjax.debug.js"></script>
<script type="text/javascript">   
function TestService() 
{  
   Sys.Net.WebServiceProxy.invoke("../../Services/MVCService.asmx","HellowWord", false,null, success, fail );
}
</script>
Ahmy
Please say what part of that link solved your problem, so everyone will know.
John Saunders
Hope that this will be usefull for every one new in MVC ajax or nin MVC at all
Ahmy
+1 for editing this. Thanks.
John Saunders