Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to implement AJAX using ASP.NET MVC and jquery?
    primarykey
    data
    text
    <p>I'm trying to figure out the best way to make an AJAX call to perform some action, and then do some jquery effect magic to make it look like something is happening.</p> <p>This is the basic idea: I have a link that when the user clicks it I need to update my model to say that the user is interested in an item (on the server side). However, I would also like to fade out the link (it's a checkbox image I made), change classes around and then fade the link back in. Behavior is like this: when you're not interested in an item the link looks like an empty box, when you hover over it the check appears, when they click on it the link fades out and comes back with the check in the box, and now when they hover over it the check disappears (the behavior reverses).</p> <p>My problem is the link doesn't seem to be able to keep track of where it is (whether its in a checked state or an unchecked state).</p> <p>I've tried doing this multiple ways. </p> <p>Example A:</p> <pre><code>&lt;a href="#" onclick="oncheck(this,&lt;%= item.ID %&gt;)" class="notchecked"&gt;&lt;/a&gt; &lt;script&gt; function oncheck(link, itemId) { //the UI effects are working correctly if ($(link).hasClass("notchecked") { $(link).fadeOut(); $(link).removeClass("notchecked"); $(link).addClass("checked"); $(link).fadeIn("slow"); } { $(link).fadeOut(); $(link).removeClass("checked"); $(link).addClass("notchecked"); $(link).fadeIn("slow"); } //do ajax request using jquery //I don't really care about the results right now //This doesn't work though, it never hits my controller action $.get("Item/ChangeCheckedStatus", { itemId: itemId } ); } &lt;/script&gt; </code></pre> <p>I've also tried doing this with Microsoft Ajax libraries</p> <p>Example B:</p> <pre><code>&lt;%=Ajax.ActionLink(" ", "Item", "ChangeCheckedStatus", new { itemId = item.ID }, new AjaxOptions { UpdateTargetId="bogusTarget", OnSuccess = "oncheck" }, new { @class="notchecked" }); &lt;script&gt; function oncheck() { //the UI effects are not working correctly here if ($(this).hasClass("notchecked") { $(this).fadeOut(); $(this).removeClass("notchecked"); $(this).addClass("checked"); $(this).fadeIn("slow"); } { $(this).fadeOut(); $(this).removeClass("checked"); $(this).addClass("notchecked"); $(this).fadeIn("slow"); } } &lt;/script&gt; </code></pre> <p>With the MS AJAX libraries, I get to the controller action, and the item gets updated, but then the jquery effects don't work.</p> <p>Is there an easier way to do what I'm trying to accomplish or am I just missing something obvious here??</p> <p><strong>UPDATE</strong> This is what the controller action looks like:</p> <pre><code>public string ChangeCheckedStatus(long itemId) { //do some stuff return "What is going on???"; } </code></pre> <p>Note: other actions in the controller work fine, it's just this one (which is the only one used via AJAX btw).</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.
    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