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 :
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.
Don't forget to click "Mark as Answer" on the post that helped you.
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.
<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 + "'); }"
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
postb99
Member
2 Points
8 Posts
Parameter for AjaxOptions OnBegin callback ?
Apr 23, 2009 08:35 AM|LINK
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
Augi
Contributor
6730 Points
1142 Posts
Re: Parameter for AjaxOptions OnBegin callback ?
Apr 23, 2009 09:28 AM|LINK
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.
postb99
Member
2 Points
8 Posts
Re: Parameter for AjaxOptions OnBegin callback ?
Apr 24, 2009 10:08 AM|LINK
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
Augi
Contributor
6730 Points
1142 Posts
Re: Parameter for AjaxOptions OnBegin callback ?
Apr 24, 2009 11:17 AM|LINK
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(); }2guns
Member
4 Points
2 Posts
Re: Parameter for AjaxOptions OnBegin callback ?
May 12, 2009 08:45 PM|LINK
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 [;)]
CoreOverride
Member
2 Points
2 Posts
Re: Parameter for AjaxOptions OnBegin callback ?
Sep 07, 2009 03:59 PM|LINK
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?
tassadaque
Member
80 Points
93 Posts
Re: Parameter for AjaxOptions OnBegin callback ?
Oct 01, 2010 10:49 AM|LINK
hello augi,
<div id="pdmscst"></div>
2guns
Member
4 Points
2 Posts
Re: Parameter for AjaxOptions OnBegin callback ?
Oct 01, 2010 11:29 AM|LINK
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'); }" }