ajax.beginform call a script on success response

Jak
Jak
Member
364 Points
152 Posts

Hi,

I want to call a script after ajax form submission.

Thanks

Views: 8280
Total Answered: 1
Total Marked As Answer: 1
Posted On: 17-Sep-2015 04:28

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

Hi Jak,

Use onSuccess event of ajax form submission as:

View Code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery-1.11.3.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<title>AjaxSubmit</title>
</head>
<body>
<div>
@using (Ajax.BeginForm("AjaxSubmit2", "Default1", new AjaxOptions { LoadingElementId = "AjaxSubmitLoadingID", OnSuccess = "GetResponseResult()", HttpMethod = "Post" }))
{
<div id="AjaxSubmitLoadingID"></div>
<input name="test" />
<button>Ajax submit</button>
}
</div>
<script type="text/javascript">
function GetResponseResult(response) {
//response
}
</script>
</body>
</html>

Controller:

using MvcApplication3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
public ActionResult AjaxSubmit()
{
AjaxlModel model = new AjaxlModel();
return View(model);
}
[HttpPost]
public ActionResult AjaxSubmit2(AjaxlModel model)
{
return Content("simple Content");
}
}
}

 

In View Code GetResponseResult javascript method will call automatically when form submission success.

 

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