ajax 如何在 actionlink mvc 4 中调用 javascript 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16253876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:00:26  来源:igfitidea点击:

how to call a javascript function in actionlink mvc 4?

javascriptajaxasp.net-mvcasp.net-mvc-4

提问by Mohammadov

so i have my view that contains some actionlinks and a javascript method, what i want is call my script in the actionlink, this is my script :

所以我的视图包含一些动作链接和一个 javascript 方法,我想要的是在动作链接中调用我的脚本,这是我的脚本:

function deleteSubscriber(id)
{
  var url = '/Subscribers/Delete/' + id;
  $.ajax({
    type: "delete",
    url: url,
    data: {},
    datatype: 'json',
    success: function (data) { alert(id); },
  });
}

this is my actionlink :

这是我的操作链接:

 @Html.ActionLink("Delete", "Delete", new { id=//here i want to put my script  },new { @class = "delete-logo" })

this is my action :

这是我的行动:

[HttpDelete,ActionName("delete")]
    public ActionResult Delete(string id)
    {
        try
        {
              IEnumerable<Subscribe> list = from s in dbcontext.Subscribes select s;
              foreach (var sb in list)
              {
                  if (sb.cin == id)
                  {
                      dbcontext.Subscribes.Remove(sb);
                  }
              }
              dbcontext.SaveChanges();
              return View("Index");
        }
        catch
        {
            return View();
        }
    }

回答by PSL

There are many ways to do this, Here is one of them:-

有很多方法可以做到这一点,这是其中之一:-

In your View:-

在您看来:-

@Html.ActionLink("Delete", "Delete", new { id=//here i want to put my script  },
 new { @class = "delete-logo" });

If you want to call it ajax way you ca create your link like this.

如果您想以 ajax 方式调用它,您可以像这样创建链接。

<a class="delete-logo" data-key="@Model.Cin" href="javascript:void(0);"/>

Script:-

脚本:-

$(function()
{
   $('.delete-logo').on('click',function(){
   //Do something to get id.
   //Get the delete button id if it is the id you want to use for deletion.
   var id = $(this).data('key'); 
   deleteSubscriber(id);
  })
});

回答by Tieson T.

If you want to insist on having the JavaScript call in your anchor, you don't necessarily need/want to use the HtmlHelper. You can write plain old HTML:

如果你想坚持在你的锚中调用 JavaScript,你不一定需要/想要使用 HtmlHelper。您可以编写普通的旧 HTML:

<a href="@Url.Action("delete")" onclick="javascript:deleteSubscriber(@id)">Delete</a>

I would not recommend using this syntax, though; jQuery (and other JavaScript frameworks) provide a much less intrusive way of binding handlers:

不过,我不建议使用这种语法;jQuery(和其他 JavaScript 框架)提供了一种更少侵入性的绑定处理程序的方式:

<a href="@Url.Action("delete")" class="delete-btn" data-id="@id">Delete</a>

$('a.delete-btn').on('click', function(event) {
    event.preventDefault();
    // call the function
    deleteSubscriber($(this).data('id'));
});

Obviously, in both of these I assume you have some sort of loop where you have access to an idvariable.

显然,在这两个中,我假设您有某种循环可以访问id变量。

回答by Daniel A. White

You don't need to have it be an ActionLinkunless you wanted it to call it if JavaScript is turned off.

您不需要将它设为 an ,ActionLink除非您希望它在 JavaScript 关闭时调用它。

I would add a classattribute to the link and bind it with the .clickevent.

我会class向链接添加一个属性并将其与.click事件绑定。