简体   繁体   English

在MVC中,不会使用Ajax.ActionLink触发Javascript函数

[英]In MVC Javascript function is not getting triggered using Ajax.ActionLink

Problem Statement: Not able to trigger the Java-script Function from Ajax Action Link,but i'm able to trigger the same Java-script function using html.actionlink 问题陈述:无法从Ajax Action Link触发Javascript函数,但是我可以使用html.actionlink触发相同的Javascript函数

What i'm doing: Actually i'm having grid of details and link for delete on each row,on-click of delete i want to pass value to an action result called Delete by using Javascript/Jquery function. 我在做什么:实际上,我在每一行上都有详细信息网格和用于删除的链接,单击删除时,我想使用Javascript / Jquery函数将值传递给名为Delete的操作结果。

What i'm trying to avoid: On click of delete it should not navigate to new url as it happens with html.actionlink,so i'm using ajax.actionlink for this: 我想避免的事情单击删除时,它不应导航到新的html,因为它发生在html.actionlink中,因此我为此使用ajax.actionlink:

Please find the image for your reference: 请找到图片以供参考:

在此处输入图片说明

View: 视图:

<table class="table">
        <tr>

             <th>
                @Html.DisplayNameFor(model=> model.RequestedBy)
            </th>
             <th>
                @Html.DisplayNameFor(model=> model.Description)
            </th>
            <th>
                @Html.DisplayNameFor(model=> model.Name)
            </th>
             <th>
                @Html.DisplayNameFor(model=> model.FromDate)
            </th>
             <th>
                @Html.DisplayNameFor(model => model.ToDate)
            </th>

        </tr>
        @if (Model!= null)
        {
            foreach (var item in Model)
            {
            <tr>

                <td>
                    @Html.DisplayFor(modelItem => item.RequestedBy,new { id="lstRequestedBy"})
                    @Html.Hidden("#RequestedBy") 
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Description)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FromDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ToDate)
                </td>
                <td>    

                     @Ajax.ActionLink("Delete","Delete","Request",
                     new AjaxOptions 
                     {
                         OnBegin="return DeleteAsset()",
                         HttpMethod = "POST"
                     })
                     @*@Html.ActionLink("Delete","Delete","Request", new { onclick="DeleteAsset()"})*@
                </td>
            </tr>
            }
        }

    </table>

Javascript: Javascript:

<script type='text/javascript'>

    function DeleteAsset() {
        var RequestedBy= $('#lstRequestedBy').val();
        $('#RequestedBy').val(RequestedBy);

    }
</script>

Controller: 控制器:

  public ActionResult Delete(string RequestedBy)
  {
      //Want to perform some action here based on RequestedBy
   }

What i'm doing wrong here?? 我在这里做错了什么?

Why java-script function is not getting triggered with ajax.actionlink,which is getting triggered using html.actionlink?? 为什么java-script函数没有被ajax.actionlink触发,而ajax.actionlink是使用html.actionlink触发的?

It is also fine if any one provides alternative for this???? 如果有人为此提供替代方案也可以吗?

Personnaly, i like to do my own ajax call. 性格方面,我喜欢做自己的ajax电话。

<td>    
    // prevent the link from doing something. You can also use evet.preventDefault in the click event.
    <a href="javascript:void(0)" data-requestedby="@item.RequestedBy" >Delete</a>

</td>


$("[data-requestedby]").click(function () {
    var row = $(this).closest("tr");
    $.ajax({
         url: "Delete/Delete",
         data : { RequestedBy : $(this).attr("data-requestedby")  } ,
         success:function(result){
             // probably remove the row;
             $(row).remove();
         }
    });

});

Code not tested but i hope you get the idea. 代码未经测试,但我希望您能理解。

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

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