Hey there, I am facing an issue with passing a list in an ajax call to my controller as I am getting null data.
Below is the code snippet:
Class:
public class CurrentProcessNameAndBucketName
{
public string ProcessName { get; set; }
public string BucketName { get; set; }
public int Id { get; set; }
public int AccountId { get; set; }
public int FromProcessId { get; set; }
public int FromBucketId { get; set; }
public int ToProcessId { get; set; }
public int ToBucketId { get; set; }
public string FromRefField { get; set; }
public string ToRefField { get; set; }
public int UserId { get; set; }
}
Controller:
[HttpPost]
[Authorize]
[Route("api/Master/Workflow/InsertRefFieldClone")]
public bool InsertRefFieldClone(List<BusinessEntities.Master.CurrentProcessNameAndBucketName> item)
{
try
{
BusinessLayer.IMaster.IWorkflow a = (BusinessLayer.IMaster.IWorkflow)DALFinder.GetInstance(typeof(BusinessLayer.IMaster.IWorkflow));
for (var i = 0; i < item.Count(); i++)
{
a.InsertRefFieldClone(item[i]);
}
return true;
}
catch (Exception ex)
{
Utils.Logger.Instance.LogException(ex);
return false;
}
}
My front end:
const saveDetails = [];
const dataLength = tbody_Copy.children().length;
for (let i = 0; i < dataLength; i++) {
const saveDetail = [];
saveDetail.Id = 0;
saveDetail.AccountId = Utils.getCurrentUser().AccountId;
saveDetail.FromProcessId = ctrlProcess.val();
saveDetail.FromBucketId = ctrlBucket.val();
saveDetail.ToProcessId = processId;
saveDetail.ToBucketId = bucketId;
saveDetail.FromRefField = $('#' + tbody_Copy.children().eq(i).find('SELECT').attr('id')).val();
saveDetail.ToRefField = tbody_Copy.children().eq(0).children().eq(1).text();
saveDetail.UserId = Utils.getUserId();
saveDetails.push(saveDetail);
}
const data = saveDetails;
var access_token = Utils.getToken();
$.ajax({
url: Utils.getWebApiUrl() + "/api/Master/Workflow/InsertRefFieldClone",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data),
dataType: 'json',
type: 'POST',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'bearer ' + access_token);
},
success: function (response) {
debugger
},
failure: function (response) {
Utils.showError(JSON.stringify('Records cannot be saved please try again later.'));
},
error: function (response) {
Utils.showAlert(response.responseText);
}
});
I'm having trouble identifying the issue in my code despite reviewing various resources. Any assistance would be greatly appreciated.