Solved

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

Posted on 2011-09-26
3
585 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

776 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