简体   繁体   English

从javascript调用ASP操作

[英]call an Asp Action from javascript

I have a table that will use is populated by javascript when another table option is clicked. 单击另一个表选项时,我有一个将由javascript填充的表。 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. 所有这些都没有问题,当我将删除按钮添加到表中时,会触发onClick事件,但是在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 UPDATE2

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

You should specify http method in ajax settings. 您应该在ajax设置中指定http方法。 Try to change your javascript like below: 尝试如下更改您的JavaScript:

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: 如果您更喜欢使用data:{ id:id }则需要创建一个模型类:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM