@ Ajax.ActionLink OnComplete传递此信息

时间:2014-03-28 15:10:40

标签: jquery asp.net-mvc-3

为什么这段代码不起作用?我想传递对<a href ='' .. </ a>的引用并隐藏它

@Ajax.ActionLink("titie", "List", new { id = Model.id },
   new AjaxOptions { UpdateTargetId = "someDiv", HttpMethod = "GET", OnComplete = "change(this)")},null)

  <script type="text/javascript">  
   function change(link) {
    $(link).hide();
   }
  </script>

3 个答案:

答案 0 :(得分:0)

您需要直接隐藏HTML ID链接。您可以通过此重载为actionlink指定一个Id。

Ajax.ActionLink( string linkText,
             string action,
             object routeValues,
             AjaxOptions ajaxOptions,
             object htmlAttributes )

@Ajax.ActionLink("titie", "List", new { id = Model.id }, 
new AjaxOptions { UpdateTargetId = "someDiv", HttpMethod = "GET",
OnComplete = "change(this)")}, new {id = "linkToBeHidden"}})

现在您可以通过Id隐藏链接

<script type="text/javascript">  

     $( "#linkToBeHidden" ).on( "click", function() {
               $('#linkToBeHidden').hide();
     });

</script>

答案 1 :(得分:0)

这样做:

@Ajax.ActionLink("titie", "List", new { id = Model.id },
  new AjaxOptions { UpdateTargetId = "someDiv", HttpMethod = "GET", OnComplete = "change")},
new { id="link" })

JS:

function change(){
    $('#link').hide();
}

您的代码不起作用,因为您的案例中的this引用了ajax请求对象,而不是锚元素

答案 2 :(得分:0)

请加入 jquery.unobtrusive-ajax.js :  options.context = element;(~100行)

你可以在asp.net mvc html helpers中使用它