Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi , I wanna to call and MVC post action with this definition
C#
[HttpPost]
        public void Delete(Guid contactId)
        {
            var contact = _contactRepository.GetContactById(contactId);
            _contactRepository.RemoveContact(contact);
            _contactRepository.Save();
       
        }


in a table with ajax call , this is my delete link
HTML
<td>
    <a href=""  önclick='DeleteFunction(@item.Id))'>Delete</a>
</td>


why this script doesn't work

JavaScript
<script type="text/javascript">

    $.ajax({
        type: "POST",
        url: @Url.Action("Delete","Contact"),
        data: data,
        success: success,
        dataType: dataType
    });


</script><pre lang="Javascript">



please help me about that ,thanks
Posted
Comments
Zoltán Zörgő 14-Jan-13 8:34am    
"Does not work" has no actual meaning.
Use fiddler or ie9 developer toolbar to check what url us actually called, what is passed, and what you get as result.
ahsan22 14-Jan-13 8:42am    
actually I don't know how to do in ajax call , it should take any parameter based on my action ??
Zoltán Zörgő 14-Jan-13 8:49am    
Depend on your routing. But what I don't see is how the data is filled. Yous javascript snippet looks incomplete to me...
ahsan22 14-Jan-13 8:53am    
that is it , it is incomplete and I don't know what to do , and didn't find any related sample on web.
ahsan22 15-Jan-13 17:35pm    
thanks...

1 solution

Try something like this:
XML
<script type="text/javascript">
function DeleteFunction(guid){
    $.ajax({
        type: "POST",
        url: @Url.Action("Delete","Contact"),
        data: JSON.stringify({contactId: guid}),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        error: function (xhr) {
            alert('Error: ' + xhr.statusText);
        },
        success: function (result) {
            alert(result);
        },
        async: true,
        processData: false
    });
</script>

<a href="" onclick='DeleteFunction("@item.Id");'>Delete</a>
 
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