behzad razzaqi behzad razzaqi - 1 year ago 79
Ajax Question

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

I'm beginner in mvc
,i want to call the controller action from Ajax code ,for that purpose read this tutorial:


write this action method in my controller:

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

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

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

Answer Source

Try this instead:

 $(document).ready(function() {

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


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download