Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..

I am newbie to MVC tech. I want to call a simple method of a controller. Here it is what I did so far.

on change event of dropdownlist I am calling the controller's function.

<script>
    $(document).ready(function () {
        $('#ChangeStatus').change(function () {

            //debugger;
            var selectVal = $('#ChangeStatus option:selected').val().toString();
            var selecttext = $('#ChangeStatus option:selected').text().toString();


            $.ajax({
                type: "GET",
                url: '@Url.Action("demo", "displayDemo")',
                contentType: "application/json",
                dataType: 'json',
                data: { dispID: selectVal },
                success: function (response) {
                //alert('Calling function successfuly');
                var returnValue = response.ReturnValue;
                alert(returnValue);

            }
        });

         });
    });
</script>


"demo" is my method accepting one string parameter and returning another string. and "
displayDemo
" is the controller's name.


in controller I have

C#
[HttpGet]
       public string demo(string dispID)
       {
          string msg="showing "+dispID;
          return msg;
       }


After executing it, I am not getting desired result back. I am sure I am missing something. please help me to figure it out.

It's showing me error like
0x800a138f - JavaScript runtime error: Unable to get property 'ReturnValue' of undefined or null reference
Posted

1 solution

Hi :)

In controller try this code.

C#
[HttpPost]
       public string demo(string dispID)
       {
          string msg="showing "+dispID;
          return msg;
       }


This should work here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900