Not able to call controller method using Ajax call

Controller:

[HttpPost]
    public void DeleteState(string id)
    {
        int id1=Convert.ToInt32(id);
        var _db = new clubDataContext();
        var state = from m in _db.StateInfos where m.S_ID == id1 select m;
        foreach (var m in state)
        {
            _db.StateInfos.DeleteOnSubmit(m);
        }
        try
        {
            _db.SubmitChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }


    }

View:

<input id="state-id" type="text" value=@item.S_ID  style="display:none"/>
<td>
<a id="delete-link" href="" >Delete</a>
</td>

$("#delete-link").click(function(){
    $.ajax({
    url: '@Url.Action("DeleteState", "ManageSystem")',
    data: {'id':$("#state-id").val(); } ,
        success: function(result) {
        $('#edit-state').html("State Deleted Successfully");
    }
    });

    }     
  });

I don't know what is the reason but controller method is not called when i click on Delete. Is there Ajax problem? I have included Ajax Library.

Main thing I forgot to mention that My view is partial view and I am calling controller method from this partial view. So I think jQuery is not loaded

Answers


I think the problem is : Your controller is of type HttpPost. make sure Ajax call is of type Post. Use type: "POST" for more info visit jQuery ajax Documentation.


Finally got the solution. This question is already asked before. Sorry for that!

Is it OK to put JavaScript in Partial Views

Here I got similar type of question in stackoverflow itself.

1)MVC4 partial view javascript bundling Issue

2)javascript-not-working-in-partial-view

3)document-ready-is-not-working-correctly-in-partial-view

4)How to execute JavaScript function on PartialView load in MVC 3

So in my project, i made separate .js file for making javascript excecutable and then i simply call this js file like this.

Partial-View:

<script src="~/Scripts/stategrid.js" type="text/javascript"></script>

Need Your Help

Is it bad practice to use entity objects across all layers of a web application?

java hibernate jpa standards

I am developing a web application using a layered architecture. I have: