AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

ASP.NET MVC show data that returned from controller in view page?

I want to show data that returned from controller, in onComplete function:

function onComplete() {
    alert(); //I want to show data that returned from controller in this.
}

here is ajax link:

@Ajax.ActionLink("Upvote", "Upvote", "Author",
new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "vote_count",
    OnComplete = "onComplete",
})

And my controller:

public ActionResult Upvote(Guid QuestionID)
{
     return Content("Message");
}

Thanks.

Upvotes: 0

Views: 891

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

function onComplete(result) {
    alert(result.responseText);
}

Or if you use the OnSuccess callback:

@Ajax.ActionLink("Upvote", "Upvote", "Author",
new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "vote_count",
    OnSuccess = "onSuccess",
})

you could:

function onSuccess(result) {
    alert(result);
}

For example if your controller action returned a JSON result:

return Json(new { foo = "bar" }, JsonRequestBehavior.AllowGet);

in your Success callback you already get a javascript parsed object:

function onSuccess(result) {
    alert(result.foo);
}

Notice that this is not the case with the OnComplete callback where you always get the XHR object as parameter and it is up to you to do the parsing and so on. Also notice that the OnComlpete callback is always executed, even if the AJAX call fails which obviously is not the case with the OnSuccess callback.

Upvotes: 1

Related Questions