JamTay317
JamTay317

Reputation: 1017

call an Asp Action from javascript

I have a table that will use is populated by javascript when another table option is clicked. All of this works no problem, when I add the delete button to the table the onClick event fires but this isn't ever called in asp.net.

function DeleteLink(id) {
    $.ajax({
        url: '/PublicPages/LinkDelete/',
        data:{ id:id }
    });
}

please tell me where I've gone wrong.

I have tried

function DeleteLink(id) {
    $.ajax({
        url: '/PublicPages/LinkDelete/' + id
}

as well

UPDATE:

    [HttpPost]
    public async Task<IActionResult> LinkDelete(Guid id)
    {
        var pageId = _linkDataProvider.FindById(id).PublicPage.Id;
        _linkDataProvider.Delete(id);
        var page = await _pageDataProvider.FindById(pageId);
        var viewModel = _pageDataProvider.ConvertToViewModel(page);
        return View("Details", viewModel);
    }

UPDATE2

app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Flooring}/{action=Index}/{id?}"); });

Upvotes: 1

Views: 580

Answers (1)

adem caglin
adem caglin

Reputation: 24063

You should specify http method in ajax settings. Try to change your javascript like below:

function DeleteLink(id) {
    $.ajax({
        type = 'POST',
        url: '/PublicPages/LinkDelete/' + id
    });
}

Update

If you prefer to use data:{ id:id } then you would need to create a model class:

public class DeleteModel
{
    public Guid Id{get;set;}
}

[HttpPost]
public async Task<IActionResult> LinkDelete([FromBody]DeleteModel model)
....

Upvotes: 2

Related Questions