Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I want call a Action Method through java script when user typing in textbox.

my Search Action Method is

C#
[HttpPost]
[MultiButton(MatchFormKey = "action", MatchFormValue = "Search")]
public ActionResult Search(string SearchKey, FormCollection collection)
{
    string key = collection["txtSearch"];
    ProjectUtility OProjectUtility = new ProjectUtility();
    IEnumerable<Project> obj = OProjectUtility.SearchByKey(key);
    return View(obj);
}


Under "ProjectManager" Controller.

How i can call it using java script?
Posted

You can use jQuery ajax is as below (sample):

function AddToCart(id)
  {
    $.ajax({
      url: "/Portal/GetPurchasedPackages",
      dataType: 'json',
      data: { id: id },
      success: function(){
        alert('Added');      
      }
    });
  }


Check for More:jQuery.ajax()

I hope this will help to you.
 
Share this answer
 
v2
Comments
Ranjan.D 22-Oct-13 14:38pm    
It doesn't do live search as you key in the textbox. But a good example of Jquery Ajax.
 
Share this answer
 
C#
$(document).ready(function(){
   $("#ControlId").YourEvent(function(){
       $.ajax({
             Type:"GET/POST",
             ContentType:"application/json;charset=utf-8",
             url:'@Url.Action("ActionMethodName","ControllerName")',
             data:{name:"name"},
             dataType:"json",
             success: function(data){
                 alert(data.property1 + data.property2 + ...);
             },
             error: function(){
                 alert("Error");
             }             

       });

   });
});


Hope This Help
------------------
Pratik Bhuva
 
Share this answer
 
v2

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