Hinsura
7/6/2018 - 8:58 AM

JSON Response - v2

This Method returns JSON property names as UpperCase instead of camelCase and JSON.

xhrPromise.done(function (xhr) {
  if (xhr.length > 0) {
    searchPolicyResultsGrid.rows.add(xhr).draw();
  }
});
[HttpPost]
[ValidateAntiForgeryToken]
[HandleException]
public string SearchPolicy(SearchPolicyViewModel searchPolicyViewModel, List<string> selectedCategories)
{
  var listSearchPolicyResultSet =
      _policyManager.SearchPolicy(Mapper.Map<SearchPolicy>(searchPolicyViewModel), selectedCategories);

  var searchResultQ = _policyManager.GetAll()
                      .Where(o => listSearchPolicyResultSet.Contains(o.Id));
 
  var searchResultList = searchResultQ.ToList();
  
  var finalResultList = searchResultList.Select(p => new
  {
    p.Id,
    p.PolicyNo,
    IssueDate = MyHelpers.ConvertDateToString(p.IssueDate),
    ProposalStatus = EnumHelper<ProposalStatus>.GetEnumDescription(p.ProposalStatus),
    p.EndorsementNo,
    EndorsementType = EnumHelper<EndorsementTypeEnum>.GetEnumDescription(p.EndorsementType),
    p.RenewalNo,
    ProductName = _productManager.GetProductFirstOrDefault(p.ProductId).Result?.ToString(),
    Customer = _customerManager.GetCustomerAsyncById(p.PolicyHolderId).Result,
    InsuredName = _policyManager.GetPolicyInsuredByPolicyIdAsync(p.Id).Result.FirstOrDefault(x => x.InsuredType == "K")?.FullName
  });

  var json = JsonConvert.SerializeObject(finalResultList, Formatting.None, new JsonSerializerSettings
  {
    //DateParseHandling = DateParseHandling.DateTime,
    PreserveReferencesHandling = PreserveReferencesHandling.None,
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
  });

  return json;
}