Note that there are some explanatory texts on larger screens.

plurals
  1. POSupport for (this) in unobtrusive ajax callback?
    primarykey
    data
    text
    <p>I cannot understand what kind of object the console is logging, and how to identify the HTML object.</p> <p>Here is my scenario:</p> <p>1.<strong>The Form:</strong> </p> <p><em>!Note:</em> The Id of output <code>&lt;form&gt;&lt;/form&gt;</code> is overriden with <code>new {id="ABCDEF"}</code></p> <pre class="lang-asp prettyprint-override"><code>@using (Ajax.BeginForm("Add", "Home", new AjaxOptions { HttpMethod = "POST", OnSuccess="doWork(this)"}, new {id="ABCDEF"})) { &lt;div id="popup-container"&gt; &lt;-- Some Data to post --&gt; &lt;input type="submit" value="Save"/&gt; &lt;/div&gt; } </code></pre> <p>2.<strong>The script</strong></p> <pre class="lang-javascript prettyprint-override"><code>&lt;script&gt; function doWork(e){ console.log(e); // Log 'e' parameter, passed upper, as 'this' inside OnSuccess='doWork(this)' } &lt;/script&gt; </code></pre> <p>3.<strong>The console output</strong></p> <pre class="lang-javascript prettyprint-override"><code>Object { url: "http://localhost:13350/Home/Add?Length=6", isLocal: false, global: true, type: "POST", contentType: "application/x-www-form-urlencoded; charset=UTF-8"… } </code></pre> <p>4.<strong>Some Observations</strong>:</p> <ul> <li>The <code>e</code> variable is of type [Object] object</li> <li>Inside output of <code>console.log(e)</code>, I can find all the parameters passed to <code>Ajax.BeginForm()</code> helper. <em>Eg:</em> The generated URL; Action type, even the id - if dig deeper through [Object]'s properties.</li> </ul> <p>5.<strong>Some QUESTIONs:</strong></p> <p>1.What kind of Object is this? How can I access the generated HTML <code>&lt;form&gt;&lt;/form&gt;</code>?</p> <p>2.How to get the <code>id</code> (whose value must be <code>ABCDEF</code>) of the logged object <strong>STRICTLY</strong> through the <code>e</code> parameter inside <code>doWork(e)</code> function ? Eg: <code>jQuery(e).attr("id")</code></p> <p>Thank you!</p> <h1>Update 1 - the HTML beginning of the form</h1> <pre><code>&lt;form action="/Home/Add" data-ajax="true" data-ajax-method="POST" data-ajax-success="doWork(this)" id="ABCDEF" method="post"&gt; </code></pre> <h1>Solution:</h1> <p>If somebody is interested in, please check Daniel's J.G. post, he gave a full answer.</p> <h1>Update 2</h1> <h2>Since ASP.NET MVC 5.1, 'this' is available by default inside unobtrusive ajax library, see <a href="http://www.asp.net/mvc/overview/releases/mvc51-release-notes#thisContext" rel="nofollow">changelog</a> from Microsoft</h2>
    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.
    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