Blankman
Blankman

Reputation: 267020

With asp.net mvc, what can we put in AjaxOptions OnSuccess etc?

I don't seem to be able to reference items in the DOM using jquery.

How can I show/hide an element during a OnBegin/OnSuccess event?

How about add/remove a css class?

Upvotes: 1

Views: 2510

Answers (2)

Robert Koritnik
Robert Koritnik

Reputation: 105029

You're probably using something like:

<% using(Ajax.BeginForm("UpdateForm", new AjaxOptions{ OnBegin = "showIt", OnSuccess = "hideIt" })) { %>
    ...
<% } %>

OnBegin and OnSuccess are names of globally visible javascript functions that will be called at respected Ajax request stage. Those functions should do what you require:

function showIt() {
    // show and add a class on the same element
    $("#SomeElementID").show().addClass("some-class");
}

function hideIt() {
    // hide and remove class on the same element
    $("#SomeElementID").hide().removeClass("some-class");
}

I'm not sure about function parameters, because:

  1. I've never used this functionality - I only use HtmlHelper extension methods and write custom jQuery scripts that do Ajax requests and all that handling; one of the bad parts from my point of view are those global functions that are usually better avoided if not explicitly needed; I also suspect that a rare minority does Asp.net MVC Ajax apps this way, they usually use HtmlHelper extensions and use jQuery to manually control Ajax processing.
  2. Microsoft documentation is very scarce and doesn't talk about function parameters (or call context for that matter)

Instead of using global functions you can write anonymous functions inline (which isn't good because everything gets crammed ina single line as a string but supposedly works):

... OnBegin = "function() { $(\"#SomeElementID\").show().addClass(\"some-class\"); }" ...

So tell me if it works.

Upvotes: 2

John Farrell
John Farrell

Reputation: 24754

Hide Something:

$.ajax( //whatever
         success: function() { $('.classToShow').show(); }
)

Remove Class:

$.ajax( //whatever
         success: function() { $('#hideSomething').removeClass('aClass'); }   
         //addClass to add
)

OnBegin?

Did you mean beforeSend

$.ajax( //whatever
         beforeSend: function() { $('#hideSomething').removeClass('aClass'); }   
         //addClass to add
)

Are you sure your referencing jquery and not msajax?

Upvotes: 0

Related Questions