6

I have this properly posting the image and binding to the HttpPostedFileBase property on my view model, but when I put the same property in the view model's base class, it doesn't model bind the bytes. It still shows the image name, but not the content stream.

I have tested everything I can think of. Please help!

These pictures tell the whole story: enter image description here enter image description here enter image description here enter image description here enter image description here

Thanks in advance. Let me know if anything isn't clear.

UPDATE

I think this is a legit bug. I tried all the below suggestions (as of 1/26) before posting on SO. I also tried: 1. creating the base property as virtual and overriding it in the child class 2. creating a new property in the child class which merely gets/sets the base property

My solution, which is far from elegant, is to add a "temporary" property on the child class and first-thing in the MVC action method set the base property to it. I'm hoping a real solution presents itself and I can revert this cloodge and do the proper fix.

ekad
  • 13,718
  • 26
  • 42
  • 44
Andrew Lundgren
  • 1,046
  • 1
  • 11
  • 18
  • 1
    Do you have the attribute enctype="multipart/form-data" on your form, couldn't tell from the screenshots? – Andreas Jan 25 '15 at 17:42
  • @Andreas, Yeah, I added a screenshot. – Andrew Lundgren Jan 25 '15 at 18:12
  • Try uploading the same image in both fields and try just one image at a time. You want to make sure your "test cases" aren't affecting each other. – James Sampica Jan 25 '15 at 18:16
  • Try maybe get in method(in controllers) images like arguments, and not like property of your class – David Abaev Jan 25 '15 at 19:26
  • 1
    You might try attaching to the official source for DefaultModelBinder (https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/DefaultModelBinder.cs) to see what happens to the property. – Nathan Taylor Jan 26 '15 at 14:50

1 Answers1

0

If it was me, I'd create a v.simple custom binder for the model class and hand crank it. Probably be easier to do that (and more obvious for future maintenance) than try to unpick the foibles of the DefaultModelBinder. Further, that would mean you wouldn't have to (unnecessarily) pollute your model structure /controller actions / unit tests.

Here's a good article on how to create a binder based on the default model binder: http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder.

HTH

Andrew Hewitt
  • 321
  • 1
  • 6