Solved

MVC3: How do I call a controller action from JavaScript?

Posted on 2011-09-26
3
588 Views
Last Modified: 2012-05-12
I nneed to write a JavaScript function that can call mt Controller's Action and use the result. We are using JSON, Ajax and jqGrid. But I need to override some default behavior with my own function.

Please explain how to do that.

Thanks!
0
Comment
Question by:newbieweb
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 total points
ID: 36600927
Rather simple really.  All you need to do is use jQuery.Ajax.  I have provided an example below:
jQuery.ajax({
            type: "GET",
            dataType: "json",
            data: ({ id: myTestId }),
            url: "/controllers/action",
            cache: false,
            success: function (data) {

               if (data != null) {

 
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("Status: " + xhr.statusText + " ; Thrown Error: " + thrownError);
            }
        });

Open in new window

0
 
LVL 14

Assisted Solution

by:binaryevo
binaryevo earned 500 total points
ID: 36600939
Sorry here is the controller code:

       
 [Authorize]
        [HttpGet]
        public JsonResult FilterDetails(int? id)
        {
                      var item = from a in _areaRepository.Filters(Convert.ToInt32(id))
                       select new IndexViewModel
                       {
                           blablabla
                       };

                         return Json(item, JsonRequestBehavior.AllowGet);

         }

Open in new window

0
 

Author Closing Comment

by:newbieweb
ID: 36894978
Thanks!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question