Reputation: 4178
I have function that is called in "onBegin" ajax option of ajax.actionlink
function cancelAction() {
$.ajax({
data: { p: "test", z: "value" },
url: '@Url.Action("DoSomething", "TestCtlr")',
type: "GET",
dataType: "json",
success: function (data) {
if (data.do == 0) {
// do something
}
else {
alert("error message");
}
},
}
)}
return false;
}
When i click on link, alert with error message is shown, but actionlink continue to controller action, instead of breaking it.
Here's code for actionlink
@Ajax.ActionLink("Click here",//link text
"Insert", // action name
"Projects", // controller
new { poz = item.itemID.ToString() }, // route values
new AjaxOptions() { HttpMethod = "GET", UpdateTargetID = "PrjDiv", OnBegin = "cancelAction", OnSuccess = "Initiate_Dialog" }, // ajax options
new { @class = "openDialog", @id = item.idemID.ToString() } //htmlAttributes
)
Edit
I changed code to match vekah's answer, and now it's working, except i can't get redirection to work.
I tried to put $.ajax(this.url)
and $.ajax(this.href)
instead of //do something
, but it's not working...
Can someone help with this?
Upvotes: 0
Views: 775
Reputation: 990
The problem is that you return false from your anonymous success ajax function. And you want to return false from cancelAction.
You should delete those return false;
and put it after the ajax declaration.
Edit: Ok for code sample.
function cancelAction()
{
$.ajax({
data: { p: "test", z: "value" },
url: '@Url.Action("DoSomething", "TestCtlr")',
type: "GET",
dataType: "json",
success: function (data) {
if (data.do == 0) {
// DO SOME REDIRECTION BECAUSE EVENT WILL BE PREVENT
}
else {
alert("error message");
}
}
});
return false;
}
Edit²: You got some syntax errors ! Is this your true code ? Do not the console throw you some errors ? (the end of your ajax looks strange, it should be something like : });
as I correct in my code.
Upvotes: 1