Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-26
3
Medium Priority
?
604 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
binaryevo earned 2000 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

610 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