Click here to Skip to main content
15,118,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASM
Hi

i am developing web application in visual studio 2012 , ultimate..and i use ajax -post in jquery to pass values to controller.

how to call method in HomeController.cs from jquery ajax - json..

pls give example code to call
Posted

$.ajax({
async: false,
cache: false,
type: "POST",
url: "@(Url.Action("YourAction", "YourController"))",
data: { "id": ui.item.Id },
success: function (data) {
}
});
response(customer);
}


SQL
[AcceptVerbs(HttpVerbs.Post)]
       public JsonResult ActionName(int id)
       {
           ....Your Code....
           return Json(model);
        }
   
Comments
maulikshah1990 10-Apr-14 8:24am
   
Hi,,thanks,, but i found the solution,
i use


"Home/FuncName" in url

and in HomeController.cs , i write as

[HttpPost]
public string GetData(string CityValue)
{
return CityValue+"asdf";
}

this works..
Yogesh Kumar Tyagi 10-Apr-14 8:30am
   
ok, Happy Coding....
Below is sample code to make ajax –post call

//assign the value which you want to post
var value;

$.ajax({
    url: '/HomeController/YourMethodName',
    type: 'POST',    
    dataType: 'json',
    data: { id: 'value' },
    success: function (data) {
        alert("success");
    },
    error: function () {
        alert('error');
    }
});

For more information visit:
https://api.jquery.com/jQuery.ajax/[^]
http://stackoverflow.com/questions/13377139/jquery-ajax-post-in-mvc3[^]
   
Use the following code in javascript


JavaScript
$.ajax({
  type: "POST",
  url: "http://domain:portno/ControllerName/MethodName",
  data: { name: "abc", age: "20" },
  success:function(response){
   //your code goes here
  }
})
   
Comments
maulikshah1990 10-Apr-14 5:59am
   
Hi,,thanks,but when i used as above , i.e.

url: "http://localhost:1435/Home/GetData",


it gives me error as




The view 'GetData' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/GetData.aspx
~/Views/Home/GetData.ascx
~/Views/Shared/GetData.aspx
~/Views/Shared/GetData.ascx
~/Views/Home/GetData.cshtml
~/Views/Home/GetData.vbhtml
~/Views/Shared/GetData.cshtml
~/Views/Shared/GetData.vbhtml


Server Error in '/' Application.
The view 'GetData' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/GetData.aspx
~/Views/Home/GetData.ascx
~/Views/Shared/GetData.aspx
~/Views/Shared/GetData.ascx
~/Views/Home/GetData.cshtml
~/Views/Home/GetData.vbhtml
~/Views/Shared/GetData.cshtml
~/Views/Shared/GetData.vbhtml
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The view 'GetData' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/GetData.aspx
~/Views/Home/GetData.ascx
~/Views/Shared/GetData.aspx
~/Views/Shared/GetData.ascx
~/Views/Home/GetData.cshtml
~/Views/Home/GetData.vbhtml
~/Views/Shared/GetData.cshtml
~/Views/Shared/GetData.vbhtml

Source Error:
Murali Gowda 10-Apr-14 6:02am
   
Can you check what that GetData method is returning??
Is it JsonResult or View?
If its View then check the corresponding cshtml or aspx page is available
maulikshah1990 10-Apr-14 6:37am
   
hi..thanks for quick reply...but there is no cshtml page for GetData as i am calling a function/method which is called like webservice/web method..
Murali Gowda 10-Apr-14 6:52am
   
Then obviously it will throw exception.
What are you returning from GetData method??
If you are returning View then change it to JsonResult and return the data you wanted to return
Murali Gowda 10-Apr-14 6:53am
   
Is it possible for you to share the GetData method's code?
make function as string in Homecontroller and in jquery ajax , call as Home/FuncName..
   

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