HttpPostedFileBase is 0 bytes on POST for only the base view model property
Asked Answered
D

1

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.

Dysprosium answered 25/1, 2015 at 17:12 Comment(5)
Do you have the attribute enctype="multipart/form-data" on your form, couldn't tell from the screenshots?Stalinabad
@Andreas, Yeah, I added a screenshot.Dysprosium
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.Deformed
Try maybe get in method(in controllers) images like arguments, and not like property of your classLuxate
You might try attaching to the official source for DefaultModelBinder (github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/…) to see what happens to the property.Paravane
E
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

Ecclesiasticism answered 11/2, 2015 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.