Search code examples
c#asp.net-mvcjqueryrazoractionlink

How can I get sender tag on OnBegin function?


I have a list that each row has a link created by Ajax.ActionLink. I want to access to tag which clicked on OnBegin function and convert it to jQuery object.

Thanks :)

Update

this is my action code that creates a link:

@Ajax.ActionLink("linkText", "action", new AjaxOptions()
{
    OnBegin = "myfunction",
})

this is the javascript function:

<script type="text/javascript">
   function myfunction() {
     //I need refer to link that clicked
   }
</script>

Solution

  • I suggest you to use this function for ajax.actionlink

    public static MvcHtmlString ActionLink(
        this AjaxHelper ajaxHelper,
        string linkText,
        string actionName,
        Object routeValues,
        AjaxOptions ajaxOptions,
        Object htmlAttributes
    )
    

    In the htmlAttributes, you can give the new {class='lnk'}

    Use jQuery to find the link which you are clicked using $(this) You can also get the parent, next html elements.

    Eg (jQuery):

    $('.agree').live("click", function(){
            var currentId2 = $(this).parents(".user").attr('id');
            alert (currentId2);
            call the function() you are supposed to call in ActionBegin
    
    });
    

    A sample demo, in that I have lot of div and I have a link on each. I need to find the clicked link (all links are having class with name as agree.

    Another references

    How to use $(this) inside MVC3 Ajax.ActionLink OnBegin,OnComplete Events