Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now