Wednesday, September 7, 2016

How to access the MVC controller using Jquery to get things done.

I have to do a task using jquery and calling controller method. Following is the code to achieve it.

1) div click.
2) the value is take from the "td".
3) passed to the ajax method.
4) show result in another grid.

Jquery part

$('#mainDiv').on('click', '.child-class table tbody tr', function () {
                var Code = $(this).find("td:first-child").text();
                    $.ajax({
                        type: "POST",
                        async: true,
                        datatype: "json",

                        url: "/Controller/Method",
                        data: { "Code": "" + Code + "" },
                       
                        success: function (data) {

                            $("#ToolTipCode").text(data);
                        },
                        error: function (x) {

                            $("#ToolTipCode").text("Doesnot exist.");
                        }
                    });

                });

MVC controller

[HttpPost]
        public ActionResult Method(string Code)
        {
            // do your stuff
            return Json(codeWithDescription);
        }