Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
"return Json(true,JsonRequestBehavior.AllowGet)" returns 'true' on screen instead of calling Onsuccess Method



I am calling Action method from Ajax.BeginForm("ActionMethod",new AjaxOptions{OnSuccess="MyScript",HTTPMethod="POST",OnFailure="MyScript2"})

What I have tried:

I tried returning Json as true first it didnt work so i tried returning the bool variable itself. Alas! it didnt.
Posted
Updated 20-Feb-16 3:56am
v3

1 solution

Follow below steps to implement AJAX.BeginForm in MVC

Step1: Enable Unobustrive in web.config
XML
<appsettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appsettings>

Step2: Add following code in your view page(*.cshtml)
HTML
@using (Ajax.BeginForm("PerformAction", new AjaxOptions { OnSuccess = "OnSuccess", HttpMethod="POST", OnFailure = "OnFailure" }))
{
    <fieldset>
        <input type="submit" value="Submit" />
    </fieldset>
}

<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script type="text/javascript">

    function OnSuccess(response) {
        alert(response);
    }

    function OnFailure(response) {
        alert("Something wrong happens. Error is: " + response);
    }

</script>

Step3: Add following code in Controller's action
C#
[HttpPost]
public JsonResult performAction()
{
	return Json(true, JsonRequestBehavior.AllowGet);
}
 
Share this answer
 
Comments
Abrar Kazi 20-Feb-16 9:57am    
I already have included them. But it doesn't work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900