Quick Inquiry

Home >> Articles >> How To Fix AJAX JSON MVC Method Not Being Called In Asp.Net Web Development?

Developers work on major issues and get them resolve for availing proper functioning application. In this post, asp.net web development team will explain the way to resolve issue related to not calling controller method. The experts will use AJAX JSON call from view page.


Today, I will explain how to resolve problem related to not calling controller method using AJAX JSON call from view page.


I see that many of users facing the issue that he or she cannot get client call of controller method when using AJAX in view page.


Two most common issues as below.

  1. Controller method not being called by AJAX call.
  2. Getting null parameter values on controller method.

So, now we will get detail information about these problems and then I will give the solution to resolve issues.


Ajax Json MVC Method

Controller method not being called by AJAX call.

This issue will rise when you want to pass model call values to controller method, or there is a difference between AJAX call type and controller method type that is PSOT and GET.So, for that you have to be sure that the type of AJAX call and Controller method are same.

Some times it will not work when you initialize a button with “button” type and not with “submit” type. When you set type of button to “Submit” on page, then you have to set calling method on “using” statement with HttpMethod type as below.

This is one type of AJAX call.

using (Ajax.BeginForm("RegisterCustomer", controllerName, newAjaxOptions { HttpMethod = "GET", UpdateTargetId = "divBody", InsertionMode = InsertionMode.Replace })) { //your other html controls are here. <inputtype="submit"id="btnRegister"name="btnRegister"value="btnRegister"/> }

You can see that I am calling RegisterCustomer method with GET type of HttpMethod and my button type is “submit”. It will surely call a controller method and you will get all html control values in parameters and also you will get model class values to class parameter of method.

Second type of AJAX call is java-script call.

$.ajax({ url: "@Url.Action("RegisterCustomer", "Customer")", type: "GET", data: dataToSend, success: function (data) { $(".content_int").html(data); returnfalse; }, complete: function () { $("#ajax-loader").hide(); } });

Here is the model class

publicclassCustomer { publicstringcustomerName { get; set; } publicstringcustomerAddress { get; set; } publicstringcontactNo { get; set; } }

Here you can see that I passed “dataToSend” is object type data that is define as below. Properties for these object are same as model class.

vardataToSend = { customerName: $(‘#txtName’).val(), customerAddress: $(‘#txtAddress’).val(), contactNo: $(‘#txtMobileNumber’).val() };

These both methods will work and call controller method.


Getting null parameter values on controller method.

This issue will occurs when you are trying to call HTTPGET type of control method using button type of input html control. You have to be sure when you are calling HTTPGET controller method, you have input type button control with type=submit.

<inputtype="button"id="btnRegister"name="btnRegister"value="btnRegister"/>

Replace above line with this.

<inputtype="submit"id="btnRegister"name="btnRegister"value="btnRegister"/>

The above example is same for this issue also.


Some times when you don’t define contentType to $.ajax call, it will not call any type of controller method, so you have to be sure when you are passing JSON type object data to controller you have to define contentType as below.

contentType: 'application/json; charset=utf-8',

Now finally, following is the order of properties for AJAX call in view or any java-script call with example.

vardataToSend = { customerName: $(‘#txtName’).val(), customerAddress: $(‘#txtAddress’).val(), contactNo: $(‘#txtMobileNumber’).val() }; $.ajax({ url: "@Url.Action("RegisterCustomer", "Customer")", type: "GET", dataType: "string", async:true, //asynchronous call type true or false, default is true, data: dataToSend, contentType: 'application/json; charset=utf-8', success: function (data) $('#lblMsg').val(data); //display success message in label. }, complete: function () { //Here you can execute any action that you want after completion of ajax call. }, error: function (xmlHttpRequest, errorText, thrownError) { //Here you can execute any action if controller method throw any error. } });

And controller method is like this.

[HttpGet] publicActionResultRegisterCustomer(Customer model) { try { ………Your logic here. } catch (Exception ex) { if (log.IsErrorEnabled) log.Error(ex.Message, ex); } returnJson(r, JsonRequestBehavior.AllowGet); }

Please note that the coding shared by asp.net web development team is for reference purpose only. If you have any doubt or want to ask anything related to the issue, make comments below.


For further information, mail us at info@aegissofttech.com


Copyright © 2017 - aegisisc, All rights reserved