5

I have the following model objects

[DataContract]
public class Filter
{
    [DataMember (Name ="start")]
    public int Start { get; set; }

    [DataMember (Name="rows")]
    public int Rows { get; set; }

    [DataMember(Name = "geoloc")]
    public GeoLocationModel GeoLocation { get; set; }

}

[DataContract]
public class GeoLocationModel
{
    [DataMember(Name = "lat")]
    public double Latitude { get; set; }

    [DataMember(Name = "lng")]
    public double Longitude { get; set; }
}

and the following action

    [HttpGet]
    public async Task GetCities([FromQuery]Models.Filters.Filter filters)
    {
        //some code
    }

When I call my action with the following query (query generated by axios):

   /api/cities/?start=0&rows=5&geoloc=%7B%22lat%22:44,%22lng%22:3%7D

Parameters Start and Rows are filled correctly but GeoLoc is null.

How do I need to format my query to satisfy FromQuery Attribute?

8
  • 1
    Why dont you use POST ? Commented Sep 12, 2019 at 16:11
  • I also would suggest post but as an add-on you could set a hidden field with the JSON value of your complex model and post it back as a string. On your controller action just have a string parameter, get the JSON and deserialize it. Commented Sep 12, 2019 at 18:07
  • Try /api/cities/?start=0&rows=5&geoloc.lat=44&geoloc.long=3 Commented Sep 13, 2019 at 2:55
  • @TonyNgo I know I could use a POST, but it's a query to retrieve objects without modify them so it should be a GET Commented Sep 13, 2019 at 6:40
  • @ChetanRanpariya I already tried, it doesn't work Commented Sep 13, 2019 at 6:40

1 Answer 1

3

You could try url like:/api/cities?start=4&rows=5&geoloc.Latitude=4&geoloc.Longitude=3

Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work But it works if I use /api/cities?start=4&rows=5&geolocation.Latitude=4&geolocation.Longitude=3 It means that FromQueryAttribute doesn't take in account DataMember attributes :(
Yes.Did you change the model?Actually your property name is geoloc not geolocation in your question.
oh sorry for the mistake, my property name is GeoLocation
So this thread seems to be resolved.If you really want to use DataMember attributes,I suggest that you could mark this thread to close it and ask a new thread with posting necessary code.
This is not completly resolved because I would like to use my datamember names.

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.