1

I'm having an issue with my parameter not binding when I send a request to my API endpoint when passing a query string, the parameter binds if I flatten Submission but this isn't the solution I want. I can't provide the client-side code that sends the request as a third party control handles the request.

I've supplied an example of the query string sent with the request that can be seen in developer tools, however, Person and Property come back as null. Does anyone have any suggestions, specifically server-side changes that I can do to bind this complex object? thanks.

public class Submission {
    public Person Person {get; set; }
    public Property Property { get; set; }
}

public class Person {
   public int Age { get; set; }
}

public class Property {
   public string Address { get; set; }
}

Query string parameters

'Person[Age]': 18
'Property[Address]': test

 [HttpGet("action")]
 public async Task<IActionResult> Submit([FromQuery] Submission model)
 {

 }
1
  • 2
    Try Person.Age in the querystring. Commented Nov 11, 2020 at 22:33

2 Answers 2

6

In this case the URL you want to call is:

https://localhost:.../.../action?Person.Age=18&Property.Address=test
Sign up to request clarification or add additional context in comments.

Comments

0

Change your action :

[HttpGet("Submit")]
public async Task<IActionResult> Submit([FromQuery] Submission model)
{

}

Try this for your Submission model

public class Submission {
    public int Age {
        set { 
           if (Person == null) Person = new Person();
           Person.Age = value;
        }
    }
    public string Address {
        set { 
           if (Person == null) Person = new Person();
           Person.Address = value;
        }
    }
    public Person Person { get; set; }
    public Property Property { get; set; }
}

or you can create

public class SubmissionModel : Submission
{
    public int Age {
        set { 
            if (Person == null) Person = new Person();
            Person.Age = value;
        } 
    }
}

in this case you action will be

[HttpGet("Submit")]
public async Task<IActionResult> Submit([FromQuery] SubmissionModel subModel)
{
    var model = subModel as Submission;
    // other code here
}

In this case you have to call this URL:

https://localhost:.../.../submit?Age=18&Address=myaddress

1 Comment

As mentioned in the original post, flattening the object does work, however, my example is a simplified version and I'd prefer to not flatten my multinested class.Thanks for the suggestion though!

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.