Home » Asp.netRSS

Parameter for AjaxOptions OnBegin callback ?

 Hello,

 I'm using asp.net mvc v 1.0 and wondering how I could write the AjaxOptions block so as to pass a parameter to the OnBegin callback. I write relevant code at bottom.

I tested "Test('hello')" but this prevented the ajax call to correctly update the target area (response opened in a new page).

However, when I look at the arguments passed to the callback, I find some, and just miss the correct documentation page. Could someone point me out to it please ? Argument $4 is the JQuery (it seems) object that will be updated by the ajax call. I can do with it, but will it remain in 4th position ? So I need to grab the correct information.

Here are the callback parameters when looking at it with Firebug :

Object $3=Object $4=tbody#d1.tbody_nav $0=0

Here is my code :

------------

string lien = Ajax.ActionLink(nomAffiche, "Lire",
    new
    {
       (skip some request parameters here)
    },
    new AjaxOptions {
        UpdateTargetId = "d1",
        HttpMethod = "post",
        LoadingElementId = "chargement_d1",
        OnBegin = "Test"
    },
    (skip some html attributes here)
    );

 ----------

function Test(chaine) {
    alert('test' + chaine);
}

-------------

 Thanks,

 Barbara

 

7 Answers Found

 

Answer 1

Check code  in Scripts/MicrosoftMvcAjax.debug.js file in your project - the code is self-explanatory. The parameter  for all these callbacks is instance of Sys.Mvc.AjaxContext class. It has some get_* method that can be usefull. Look at this class yourself - it's placed at the start of the file.

 

Answer 2

Hello Augi,

Thanks for your hint. It's not really easy for me but I'm trying to go through :)

 In the callback  code, I looked at :

arguments[0].get_request() (documentation page  : http://msdn.microsoft.com/fr-fr/library/bb310979.aspx)

It seems it's the way to go, acess some data I've previously put in the request  with a given key :)

But in my case, and I guess I will have troubles with it, I have an Ajax.ActionLink and not Ajax.BeginForm.

It seems I have to parse : arguments[0].get_request().get_url() ?

Or use the user context instead ? If so, how to put some data inside ? I would better rely on <% ActionLink %> tag and not to write  too much "geek" js code.

So thank you for helping me again.

 

 EDIT : now I would use the "OnComplete" of AjaxOptions rather than the "OnBegin" as stated in first post. I guess things are not very different.

 

Barbara

 

Answer 3

If you want to read request  url only in OnComplete then OnComplete function  should look like this:

function myFunction(ajaxContext)
{
   var url = ajaxContext.get_request().get_url();
}

If you want to store userContext into Sys.Net.Request object  then it can be done in OnBegin function:

function yourFunction(ajaxContext)
{
  ajaxContext.get_request().set_userContext("your context information");
}

This context can be read easily in OnComplete:

function myFunction2(ajaxContext)
{
  var yourContext = ajaxContext.get_request().get_userContext();
}
 

Answer 4

Hi. I figured it out :).

<script type="text/javascript">
function onDelete(obj)
{
    if (confirm('Are you sure?'))
    
{
        // this is just how I modifying inner HTML of <small> tag - just text without the link to delete
        obj.parentNode.innerHTML = '(deleting...)';
        return true;
   
}
    else
       
return false;
}
</script>

SOME MESSAGE <small>(<%= Ajax.ActionLink("delete", "Delete", "Wall", new { id = wallPost.ID }, new AjaxOptions { UpdateTargetId = "wall", OnBegin = "function(){ return onDelete(this); }" })%>)</small>

Of course, you can pass  some dynamic values OnBegin = "function(){ return onDelete('wp" + wallPost.ID + "'); }"

 Hope it helps Wink

 

Answer 5

Thanks, I think its very useful.

Additionally, how I can send client-side values using ActionLink?

For example I want to send name and html  into my server method which looks like:

        [AcceptVerbs(HttpVerbs.Post)]
        public void CreateTemplate(string name, string  html) { ... }

and AjaxOptions:

..., new AjaxOptions { OnBegin = "function(request) { onGetTemplateHTML(request,'testName'); }" }

and JS method:

function onSave(request, name) {
    request.get_request()._body += "&html=" + encodeURIComponent(tinyMCE.activeEditor.getContent())+"&name="+name;
}

So, is more creal solution exists to map client variables to server method's parameters?

 

Answer 6

hello augi,


Augi:

If you want to read request  url only in OnComplete then OnComplete function  should look like this:

function myFunction(ajaxContext)
{
   var url = ajaxContext.get_request().get_url();
}



this is perfectly ok if we have one callback  per ajax.actionlink. but what if i have four ajax.ActionLinks
and i want to execute same callback named processRequest(context, myparameter){}
inside this function i will check value of myparameter and perform some actions accordingly.
my question is how can i pass  second paramter to onComplete or onSuccess callback functions
 

Answer 7

I hope piece of code  in my post  is what are you looking for? Two posts up.

EDIT:

Just for sure:

new AjaxOptions { UpdateTargetId = "wall", OnBegin = "function(context) { return onDelete(context, 'secondParameter', 'thirdParameter'); }" }

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter