Afsaneh Afsaneh - 13 days ago 6
jQuery Question

How to pass dom element beside output value in ajax.actionLink's success function?

I have an Ajax.ActionLink which sends a request to server and gets a result from server. So in AjaxOption I just write name of success function.

OnSuccess = "SuccessDeleteFunction"


but based on result I want to change DOM element.To pass current DOM element in success function I used this code:

OnSuccess = "SuccessDeleteFunction(this)"


But How can I have Both at the same time.

function SuccessDeleteFunction(result, tag) {

if (result.Success) {
// do sth
}
eval(result.Script);
}


Here is my code:

@foreach (var item in Model)
{
<tr class="ContentRow">
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Ajax.ActionLink(" ", "Delete", "Product", new { id = @item.Id }, new AjaxOptions() { Confirm = "Are you sure?", HttpMethod = "get", OnSuccess = "SuccessDeleteFunction" }, new { @class = "btn fa fa-trash text-danger fa-2x " })

</td>
</tr>
}

Answer

You have to your item ID to TR's like :

@foreach (var item in Model)
    {
        <tr class="ContentRow" id="tr_'@item.id'">
             <td>
                 @Html.DisplayFor(modelItem => item.Name)
             </td>
              <td>
                 @Html.DisplayFor(modelItem => item.Email)
              </td>
               <td>
                 @Ajax.ActionLink(" ", "Delete", "Product", new { id = @item.Id }, new AjaxOptions() { Confirm = "Are you sure?", HttpMethod = "get", OnSuccess = "SuccessDeleteFunction" }, new { @class = "btn fa fa-trash text-danger fa-2x " })

              </td>
        </tr>
   }

And in your JS code you have to do this, And one thing you need more is Return your item ID from SERVER RESPONSE

function SuccessDeleteFunction(result) {

    if (result.Success) {
        // also return your that id from server in response....
        $("tr_" + result.id).remove();
    }
    eval(result.Script);
}
Comments