sarpay
7/31/2018 - 9:24 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
  });

  // setting Formatting.None returns prop key names as pascal case.
  var json = JsonConvert.SerializeObject(finalResultList, Formatting.None, new JsonSerializerSettings
  {
    PreserveReferencesHandling = PreserveReferencesHandling.None,
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
    DateParseHandling = DateParseHandling.DateTime,
    Converters = { new IsoDateTimeConverter() }
  });

  return json;
}