January 8, 2019 11:41 by
Peter
In this tutorial, let me show you how to call ajax method in ASP.NET MVC.
Controller code here
[HttpGet]
public JsonResult GetAjaxData(string newCode) {
return Json(new {
Status = "Ajax called succesfully", Message = "Message show succesfully"
}, JsonRequestBehavior.AllowGet);
}
View Code Here
@ {
ViewBag.Title = "Index";
} < script src = "~/Scripts/jquery-1.8.2.js" > < /script>
<script>
function AjaxCall()
{
$.ajax({
async: false,
url: $('#ajaxURL').val(),
type: 'GET',
cache: false,
data: {
newCode : "Put the data here"
},
success: function (result) {
if(result.Status !=''){
alert(result.Status);
}
else{
alert('Ajax called failed');
}
},
error: function (result, textStatus, errorThrown) {
alert(errorThrown);
}
})
}
</script > < div > < button value = "Ajax Called"
id = "btncalled"
onclick = "AjaxCall()" > Ajax Called < /button>
</div > @Html.Hidden("ajaxURL", Url.Action("GetAjaxData", "Demo"))
App_Start-> route config.cs code here
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Demo", action = "Index", id = UrlParameter.Optional
});
}
I hope it works for you! Good luck!
HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.