简体   繁体   English

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

[英]how to call a javascript function in actionlink mvc 4?

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 : 所以我的观点包含一些actionlinks和一个javascript方法,我想要的是在actionlink中调用我的脚本, 这是我的脚本:

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 : 这是我的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();
        }
    }

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);
  })
});

If you want to insist on having the JavaScript call in your anchor, you don't necessarily need/want to use the HtmlHelper. 如果您想坚持在锚点中进行JavaScript调用,则不一定需要/想要使用HtmlHelper。 You can write plain old HTML: 你可以写简单的旧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 id variable. 显然,在这两个中我假设你有一些循环,你可以访问id变量。

You don't need to have it be an ActionLink unless you wanted it to call it if JavaScript is turned off. 除非您希望在关闭JavaScript时调用它,否则您不需要将它作为ActionLink

I would add a class attribute to the link and bind it with the .click event. 我会在链接中添加一个class属性,并将其与.click事件绑定。

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

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