Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net web app 3 28
web page freezes after ajax post 7 54
Can anyone help me find this preloader? 51 48
Javascript: Range object 16 17
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

696 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