3

I'm trying to bind to a complex object via Query Parameters, and I only seem to be binding the first property, and everything else is initialized with defaults (0, null, false, etc. . . ).

The HTTP Request is coming in with all the applicable property names and values. The property names are an exact match. It binds the first property in the query parameters, but not the others. When the request comes in & is replaced with &amps;. From what research I've done online these should be the same in the eyes of the model binder. Here is my current setup

public class MyComplexObject
{
    public string Id { get; set; }
    public int Level { get; set; }
    public Enum MyEnum { get; set; }
}
[HttpGet]
public IActionResult MyAction( [FromQuery] MyComplexObject model)
{
    // stuff done here.  model.Id is set to the correct value, everything
    // else is set to an initialized value based on type. 

    return View("view", model)
}

This is in an MVC .Net Core 3.0 Web Project (not an API).

When I debug on the action and look at this.Request.QueryString It shows "?Id=2100&Level=6&MyEnum=MyEnumValue" So values are being passed in, I just can't get it to bind to the rest of the properties, and it correctly binds the first property in the Query string (Id).

All the sources I can find say this is how I should set up a complex object via QueryParameters, but I can't find anything decisive on if this is broken for .Net Core 3.0, everything I read is for .Net Core 2.2 and earlier, as well as for POST and WebApi's.

Is this possible in .Net 3.0? If so, what am I doing wrong? There are other ways to go about doing this, I can set up route parameters that bind correctly, but I'm going to be doing this a bunch, and I was hoping to just pass in my model and be done with it rather than url/{paramters1}/{parameter2}/...

Edit::

When I open the chrome debugger and click on the Network tag the call looks like so.

{myurl}?Id=2100&Level=6&MyEnum=MyEnumValue so it matches the Request in the debugger.

Edit Part 2:

I'm making the request via a jQuery .load({url}) in the client. I did a hack var url = url.replace('%amp;', '&'); and that solved one of my problems and it correctly bound the property.

Thank you all for the help!

4
  • 1
    It looks like something is breaking the URL, it defintely shouldn't have & instead of &. That tells the model binder that the property name is amp;Level, which is isn't. Whatever is building the URL and directing the client to make the request is somehow HTML-encoding your values. The problem is before you get to this code. (Note: You can quickly test this by manually entering a corrected URL and seeing how the model binds.) Commented Nov 28, 2019 at 16:13
  • 1
    How did you make the request? It looks like the URL was mangled by the client. Did the client code use HTML encoding on the entire string instead of individual parameters perhaps? Commented Nov 28, 2019 at 16:30
  • For your Question About URL/{parameter1}/... this link may help you learn.microsoft.com/en-us/aspnet/core/fundamentals/… Commented Nov 28, 2019 at 16:32
  • Ok, it's definitely the &. I'm making the request via a jQuery .load({url}) in the client. I think I can work this through now. Commented Nov 28, 2019 at 16:35

2 Answers 2

3
public class MyComplexObject
{
[FromQuery(Name = "Id")]
public string Id { get; set; }
[FromQuery(Name = "Level")]
public int Level { get; set; }
[FromQuery(Name = "MyEnum")]
public Enum MyEnum { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you open the network in chrome debugger and add the request to the details please?
It matches the request in the debugger. I'm thinking it has something to do with &amps;
the query should be like this "?Id=2100&Level=6&MyEnum=MyEnumValue" I dont know from where come the 'amp;'
0

I was making the request via a jQuery .load({url}) in the client. I did a hack

var url = url.replace('%amp;', '&');

and that solved one of my problems and it correctly bound the property.

Comments

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.