Monday, 25 April 2016

Sending Json Data to Controller and return json data

No comments
View
---------

$('#Client').click(function () {
               $.ajax({
                            method: "GET",
                            url: "@Url.Action("GetClients", "Home")",
                            data: { 'Name': 'Vikas', 'Code': '4049' },
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (result) {
                                if (result != undefined && result != null) {
                                  alert('ok');
                                }
                                else {
                                  alert('not found');
                                }
                            },
                            failure: function () {
                               alert('error');
                            }
                        });
               });



controller.cs
------------------

 public string GetClients(string Name, string Code)
        {          
            UnitOfWork uow = new UnitOfWork();
            var postalData = uow.Repository<GetClientRepository>();
            var result = postalData.GetClient(Name, Code).FirstOrDefault();
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }