How can I return a JSON result to a Ajax.BeginForm

Jak
Jak
Member
364 Points
152 Posts

Hi,

I am working with ajax for submission in mvc and I want to return json content,

My view code as:

@{
ViewBag.Title = "View1";
Layout = "~/Views/_LayoutPage1.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>AjaxSubmit</title>
</head>
<body>
<div>
@using (Ajax.BeginForm("AjaxSubmit2", "Default1", new AjaxOptions { LoadingElementId = "AjaxSubmitLoadingID", HttpMethod = "Post" }))
{
<div id="AjaxSubmitLoadingID"></div>
<input name="test" />
<button>Ajax submit</button>
}
</div>
 
</body>
</html>

How can I return a JSON result to a Ajax.BeginForm

Views: 8795
Total Answered: 1
Total Marked As Answer: 1
Posted On: 17-Sep-2015 04:15

Share:   fb twitter linkedin
Answers
Brian
Brian
Participant
570 Points
75 Posts
         

Hi Jak,

Use OnSuccess in jsonoption of Ajax.BeginForm() as:

<div>
@using (Ajax.BeginForm("AjaxSubmit2", "Default1", new AjaxOptions { LoadingElementId = "AjaxSubmitLoadingID", OnSuccess="GetJsonResult()", HttpMethod = "Post" }))
{
<div id="AjaxSubmitLoadingID"></div>
<input name="test" />
<button>Ajax submit</button>
}
<script type="text/javascript">
function GetJsonResult(JSON) {
//JSON
}
</script>
</div>

 

And the action in the controller return json result as:

[HttpPost]
public ActionResult AjaxSubmit2(AjaxlModel model)
{
return Json("simple json");
}

 

Posted On: 22-Sep-2015 00:18
 Log In to Chat