behzad razzaqi
behzad razzaqi

Reputation: 275

Why i can not call the controller from ajax in asp.net mvc?

I'm beginner in asp.net mvc ,i want to call the controller action from Ajax code ,for that purpose read this tutorial:
CALL CONTROLLER WITH AJAX
write this action method in my controller:

[HttpPost]
        public void Test01()
        {
            string behzad = "BEHZAD RAZZAQI";

        }


and in view page write this html code:

<button type="button" id="btn1" class="btn btn-success">ثبـت نـام</button>


in on that view page write this jquery code:

<script>
        $("#btn1").click(function () {
            $.ajax({
                url: "/MainPage/Test01",
            datatype: "text",
            type: "POST",
            success: function (data) {
                alert('ok');//$('#testarea').html("All OK");
            },
            error: function () {
                $("#testarea").html("ERROR");
            }
            });
        });
    </script>


but when i fire the button i can not see any alert,what happen?how can i solve that problem?

Upvotes: 0

Views: 983

Answers (2)

Luis Teijon
Luis Teijon

Reputation: 4899

Try this instead:

 $(document).ready(function() {

    $("#btn1").click(function () {
        $.ajax({
            url:'<%= Url.Action("Test01", "MainPage") %>',
            success: function(data) {
                 alert("ok");
            }
        });
    });

 });

Make sure that MainPage is the name of your controller and that you included your jquery library, just like @bhupesh stated:

<script src="~/scripts/jquery-*.*.*.min.js"></script>

Further information can be found Here

Upvotes: 2

bhupesh
bhupesh

Reputation: 104

please make sure you have included jquery script file in your page.

<script src="~/scripts/jquery-*.*.*.min.js"></script>

Upvotes: 0

Related Questions