7

I am trying to bind data in my model posted from postman in my below model:

public class VariantModel
{
    public int Id { get; set; }     
    public List<SubvariantModel> Subvariants { get; set; }
}

public class SubvariantModel
{
    public int Id { get; set; }
    public string Description { get; set; }
    public IFormFile Document { get; set; }
}

Every property is getting populated but only Document property is coming null as you can see here :

enter image description here

But surprisingly when i inspect http request object i see that file :

enter image description here

This is how i am posting data from POSTMAN : enter image description here

enter image description here

Code :

[HttpPost]
public void Post([FromForm]VariantModel emp)
{
    var d = HttpContext.Request;
}

Can anybody tell me what could be the issue here?

Update :

enter image description here

10
  • Your key naming is very odd. The names should really be Subvariants[0].Document, not Subvariants[0][Document]. It's surprising that this actually works for Id and Description, but perhaps it simply doesn't for some reason with IFormFile. Seems odd, but I honestly don't see anything else wrong. Commented Apr 5, 2019 at 14:38
  • @ChrisPratt I tried like this Subvariants[0].Document as well but still getting null and yes indeed I am also not getting whats the problem here.Is this a bug in asp.net webapi core with model binder? Commented Apr 5, 2019 at 14:41
  • Not at all. This is very basic code, and I've run code virtually just like this. As long as the name in the post body follows the proper conventions for binding (which these seem to), then it will bind. Commented Apr 5, 2019 at 14:53
  • @ChrisPratt Then why this is not working right now.May be you have not taken IFormFile in your nested model.Is there any solution for this? Commented Apr 5, 2019 at 14:54
  • 1
    I just tested your code with ASP.NET Core 2.0 and everything worked fine for me. Could you please share your project on github and share the requests you performed via postman? Commented Apr 6, 2019 at 20:35

1 Answer 1

1

This is an issue I encountered as well. It is a known issue with nesting IFormFile as a View Model property in .NET Core v2.2.

The fix that worked for me is here.

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

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.