Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use $(this) inside MVC3 Ajax.ActionLink OnBegin,OnComplete Events
    primarykey
    data
    text
    <p>My Controller creates a list of links like this</p> <pre><code>&lt;ul id="MainMenu"&gt; @foreach (var item in Model.MenuItems) { &lt;li&gt;@Ajax.ActionLink(item.Caption, item.ActionName, item.ControllerName, new AjaxOptions { UpdateTargetId = "pageBody", OnBegin = "BeginUpdatePage", OnComplete = "EndUpdatePage", OnFailure = "FailUpdatePage" }) &lt;/li&gt; } &lt;/ul&gt; </code></pre> <p>I have some javascript like this</p> <pre><code>function BeginUpdatePage() { $("#MainMenu li").removeClass('selected'); $(this).parent().addClass('selected'); $("#pageBody").fadeTo('slow', 0.5); } function EndUpdatePage() { $("#pageBody").fadeTo('slow', 1); } function FailUpdatePage() { alert('There has been an error loading this page please try again.'); } </code></pre> <p>in the BeginUpdatePage function line 1 and 3 execute fine, but line 2 "$(this).parent().addClass('selected');" does not visiably do anything... (I have declared that class in my css).</p> <p>I have looked at Jquery docs (<a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow noreferrer">Jquery.ajax()</a>) it says that "this" is the element that was clicked. I have also checked in "jquery.unobtrusive-ajax.js" that was in my project by default. This file also passes "this" when executing my function.</p> <pre><code>"result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(this,arguments);" </code></pre> <p>What am I doing wrong here... I have seen <a href="http://www.asp.net/mvc/tutorials/iteration-7-add-ajax-functionality-cs" rel="nofollow noreferrer">other examples</a> that have used this technique but I have been failing all day!</p> <p>How can I get to the element that was clicked, inside my Begin, complete functions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload