1 Replies - 481 Views - Last Post: 07 November 2019 - 12:19 PM Rate Topic: -----

#1 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 624
  • Joined: 24-July 08

Accepting Object Array in Query String

Posted 04 November 2019 - 02:08 PM

Good Afternoon,
I have a sales page that needs to accept a BUNCH of information via GET, most of which are just string and int values. An issue that im coming across is needing the ability to pass an array of objects to the endpoint via query string. here is what the object looks like

public class CustomField
{
      public string Field{get;set;}
      public string Value{get;set;}
}



i have tried adding it to my existing object like
public List<CustomField> CustomFields {get;set;}
//and
public CustomField[] CustomFields {get;set;}



passing it as
{endpoint}?CustomFields.Field=Test1&CustomFields.Value=Test1Value&CustomFields.Field=Test2&CustomFields.Value=Test2Value

however, the controller is not populating the data. Any ideas on how i can pass an object array to an endpoint via querystring?
[edit]
Project is in C# / .NET Core 2.2 hosting in dev with IISExpress

This post has been edited by UG Cyber: 04 November 2019 - 02:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Accepting Object Array in Query String

#2 smendoza88   User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 123
  • Joined: 13-July 11

Re: Accepting Object Array in Query String

Posted 07 November 2019 - 12:19 PM

You should try [FromBody] in the controller. This is how we've used [FromBody] to capture passed in value with an ajax post call.

Take a look at the doc's here. https://docs.microso...=aspnetcore-2.2



//controller
        [HttpPost]
        public async Task<IActionResult> GetMapData([FromBody] CountyModel query)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var someData= await _someDBContext.DoSomething(query);

            return Ok(someData);
        }

//The model I'm using to capture the passed in values.
 public class CountyModel 
 {
   public int StateCode{ get; set; }
   public int Countycode{ get; set; }
 }



This post has been edited by smendoza88: 07 November 2019 - 12:21 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1