Home > Software engineering >  How to pass list of object to method by ajax MVC
How to pass list of object to method by ajax MVC

Time:11-24

Hello all I want to pass list in ajax call but in my controller I am getting null data

here is my code

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 don't understand what is wrong in my code it looks good for me but it is not working. I saw many examples and questions online and even on stackoverflow but still this problem didn't solved.

help would be appreciated.

CodePudding user response:

Try to initialize your detail with object rather then array:

const saveDetail = []; // wrong

const saveDetail = {}; // correct

see this page for reference

  • Related