1

I am trying to pass a javascript array of objects to a .NET controller action List<> parameter. Below is my C#

public bool ImportACE([FromBody] List<Element> elements)
{
    foreach(Element e in elements)
    {
        uint srcAddrInt = ConvertIpAddressToUInt(e.SRC_Addr);
        Console.WriteLine($"The IP address {e.SRC_Addr} as an integer is: {srcAddrInt}");
    }            

    return true;
}

Below is the variable I am passing and the ajax call

importData.push({
  ACLId: 11,
  DeviceId: 2,
  ACLGroupId: 5,
  ProtocolId: 27,
  LiftDate: liftDate,
  ActionDate: actionDate,
  RemedyTckt: remedyTicket,
  ITSMTckt: itsmTicket,
 /* CommandView: cleanCmdVar,*/
  SRC_Addr: srcAddr,
  SRC_Mask: "0.0.0.0",
  DST_Addr: "0.0.0.0",
  DST_Mask: "255.255.255.255",
  ArinInfo: arin,
  FlagLogInput: 1,
  FlagLog: 0,
  Active: 0
});

var jsonData = JSON.stringify(importData);

$.ajax({
  url: '/GAM/ACE/Import',
  type: 'POST',             
  data: jsonData,
  contentType: "application/json; charset=utf-8",
  success: function (result) {
    if (result) {                   
        $('#loading-overlay').fadeOut();
    } else {
        alert("Error. Import unsuccessful.");
        $('#loading-overlay').fadeOut();
    }
  },
  error: function (jqXHR, textStatus, errorThrown) {
    $('#loading-overlay').fadeOut();
    alert("Unable to import data. Error with /GAM/ACE/ImportAce ." + errorThrown);;
  }
});

Here is the model, it's rather large

 public class Element
 {
     [Required]
     public int ElementId { get; set; }
     public int DeviceId { get; set; }
     public string Device { get; set; }
     public int ACLId { get; set; }
     public string ACL { get; set; }
     public int ACLGroupId { get; set; }
     public string ACLGroup { get; set; }
     public int SequenceNum { get; set; }
     public int ActionId { get; set; }
     public bool PermitBool { get; set; }
     public bool DenyBool { get; set; }
     public string Action { get; set; }
     public string Protocol { get; set; }
     public int ProtocolId { get; set; }
     public string SRC_Addr { get; set; }
     public string SRC_Addr_Int { get; set; }
     public string SRC_Mask { get; set; }
     public string SRC_Mask_Int { get; set; }
     public string SRC_OP { get; set; }
     public string SRC_B_Range { get; set; }
     public string SRC_E_Range { get; set; }
     public string DST_Addr { get; set; }
     public string DST_Addr_Int{ get; set; }
     public string DST_Mask { get; set; }
     public string DST_Mask_Int { get; set; }
     public string DST_OP { get; set; }
     public string DST_B_Range { get; set; }
     public string DST_E_Range { get; set; }
     public int FlagLog { get; set; }
     public int FlagLogInput { get; set; }
     public bool FlagLogBool { get; set; }
     public bool FlagLogInputBool { get; set; }
     public bool FlagTCPEst { get; set; }
     public bool FlagTCPAck { get; set; }
     public bool FlagTCPFin { get; set; }
     public bool FlagTCPPsh { get; set; }
     public bool FlagTCPRst { get; set; }
     public bool FlagTCPSyn { get; set; }
     public bool FlagTCPUrg { get; set; }
     public int ICMPType { get; set; }
     public string ICMPCode { get; set; }
     public string ICMPMessage { get; set; }
     public int IGMPType { get; set; }
     public int FlagPrecedence { get; set; }
     public string Precedence { get; set; }
     public int TOSId{ get; set; }
     public int FlagDSCP { get; set; }
     public string DSCP { get; set; }
     public int FlagFragments { get; set; }
     public int DynamicTimeOut { get; set; }
     public string DynamicName { get; set; }
     public string TimeRangeName { get; set; }
     public int FlagEvaluate { get; set; }
     public string EvaluateAcl { get; set; }
     public string ReflexiveAcl { get; set; }
     public int ReflexiveTimeout { get; set; }
     public string Remark { get; set; }
     public string CommandView { get; set; }
     public DateTime? LiftDate { get; set; }
     public DateTime? ActionDate { get; set; }
     public string RemedyTckt { get; set; }
     public string ITSMTckt { get; set; }
     public string ArinInfo { get; set; }
     public int UserId { get; set; }
     public DateTime AclTime { get; set; }
     public bool Active { get; set; }
     public bool Locked { get; set; }
     public bool Permanent { get; set; }
     public int Special { get; set; }    
     public int CMDType { get; set; }
     public int CMDOther { get; set; }
     public DateTime Modified { get; set; }
     public DateTime Created { get; set; }
     public long AuthorEDIPI { get; set; }
     public long EditorEDIPI { get; set; }  
     public string Editor {  get; set; }
     public string LogOptionId { get; set; }
 }

I am not sure what I am doing wrong. The element parameter for the controller is received and shows the objects in the list. But each object's values are null. None of the data is coming through. I can see the payload coming through correctly. Any suggestions are appreciated

9
  • I have added the Element model and I fixed the issue with the controller. Thanks Commented Oct 11 at 20:11
  • 1
    why is Import function receiving a List of objects of type ACLEElement yet your class is called Element? Commented Oct 11 at 20:59
  • Yeah it should be Element here. I changed the model earlier and forgot to include that here. Code change has been above as well. The each object in the array still has all null values. No data is actually passing through Commented Oct 11 at 21:43
  • Check whether you have implemented any model binding/filter that overwrites the request body. Commented Oct 11 at 22:14
  • 2
    You have ElementId required, yet I don't see it covered in the push call site. I feel like @peter-b had commented about it, or was it another field, before he deleted his first comment. Commented Oct 12 at 22:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.