Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
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
   url: '/HomeController/MethodName,
   data: { id: id }
   success: function(data){
    alert('Sucess ' + data);      
My method is
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 17-Jan-13 9:00am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Close, but not quite how it works. Here is a sample of how it should look.
      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 );
public JsonResult YourMethod(string arg1, string arg2)
   // Do something to get a result...
   string yourResult = "Some soft of result.";
   return Json(yourResult, JsonRequestBehavior.AllowGet);
Espen Harlinn at 17-Jan-13 18:12pm
Marcus Kramer at 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)

  Print Answers RSS
0 Maciej Los 199
1 Sergey Alexandrovich Kryukov 154
2 Sascha Lefévre 120
3 Abhinav S 104
4 Rana Waqas 78
0 Sergey Alexandrovich Kryukov 6,628
1 OriginalGriff 6,298
2 Peter Leow 2,534
3 Maciej Los 2,437
4 Abhinav S 2,378

Advertise | Privacy | Mobile
Web02 | 2.8.150414.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100