Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This seems easy but is not just working for me. I have the following code to run a method in my HomeController from my javascript file
JavaScript
function(id)
 alert("here");
 $.ajax({
   url: '/HomeController/MethodName,
   data: { id: id }
   success: function(data){
    alert('Sucess ' + data);      
  }
 });

My method is
C#
public string MethodName(int id)
{
   return id.ToString() + "test ";
}



The view calling the javascript has the following scripts defined: jquery.unobtrusive-ajax.min.js, MicrosoftMvcAjax.js, MicrosoftAjax.js, validate.unobtrusive.min.js, jquery.validate.min.js

But is is not just working. Nothing happens. The first alert of the function do show up. But nothing else after that.
Posted

1 solution

Close, but not quite how it works. Here is a sample of how it should look.
JavaScript
$.ajax({
     type: 'GET',
     url: '/YourController/YourMethod',
     dataType: 'json',
     async: false,
     data: { arg1: 'arg1value',
             arg2: 'arg2value'
           },
     success: function (result) { alert(result); },
     error: function (jqXHR, textStatus, errorThrown) {
               alert( 'An error occured while loading: ' + textStatus + ": " + errorThrown + ": " + jqXHR.responseText );
           }
       });

C#
[HttpGet]
public JsonResult YourMethod(string arg1, string arg2)
{
   // Do something to get a result...
   string yourResult = "Some soft of result.";
   return Json(yourResult, JsonRequestBehavior.AllowGet);
}
 
Share this answer
 
Comments
Espen Harlinn 17-Jan-13 18:12pm    
5'ed!
fjdiewornncalwe 17-Jan-13 18:38pm    
Thanks, Espen

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