Header add

   In this article we will discuss LowerCase JSON Property Names in ASP.Net Core.

Here we will explain Controller’s Action method will be called using jQuery AJAX function and JSON data will be returned back to the View using JsonResult class in ASP.Net Core MVC.

Before start this article, please visit our previous article Paging example in ASP.Net Core MVC

Configuring the JSON Serializer setting
Add the NewtonsoftJson package into project


Then inside the ConfigureServices method, you will have to add the following code which will instruct the program to use Newtonsoft library for JSON serialization.

public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
}
view raw startup.cs hosted with ❤ by GitHub

Model
Following is a Model class named CustomerModel with two properties i.e. Name and DateTime
public class CustomerModel
{
///<summary>
/// Gets or sets Name.
///</summary>
public string Name { get; set; }
///<summary>
/// Gets or sets DateTime.
///</summary>
public string DateTime { get; set; }
}

Controller

The value of the name parameter is assigned to the Name property of the CustomerModel object along with the Current DateTime and finally the CustomerModel object is returned back as JSON to the jQuery AJAX function.

This Action method handles the call made from the jQuery AJAX function from the View.
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult AjaxMethod(string name)
{
CustomerModel person = new CustomerModel
{
Name = name,
DateTime = DateTime.Now.ToString()
};
return Json(person);
}
}

View
The View consists of an HTML TextBox element and a Button. The Button has been assigned a jQuery click event handler and when the Button is clicked a jQuery AJAX called is made to the Controller’s action method.
The URL for the jQuery AJAX call is set to the Controller’s action method i.e. /Home/AjaxMethod. The value of the TextBox is passed as parameter and the returned response is displayed using JavaScript Alert Message Box.

<div class="col-sm-6 mt-5">
<div class="col-sm-6">
<input type="text" id="txtName" class="form-control" autocomplete="off" />
</div>
<div class="col-sm-6 mt-4">
<input type="button" id="btnGet" class="btn btn-primary" value="Get Current Time" />
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnGet").click(function () {
$.ajax({
type: "POST",
url: "/Home/AjaxMethod",
data: { "name": $("#txtName").val() },
success: function (response) {
alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
});
</script>
view raw Index.cshtml hosted with ❤ by GitHub

Output

Previous Post Next Post