views:

148

answers:

1

HI,

I'm sure I'm missing something very obvious here so please forgive me.

  • I'm using MVC 2 Beta and I have a model that has several properties, strings, ints etc. the usual stuff.
  • It also has a byte array that contains an image.
  • I have an edit action method on my controller decorated with a [HTTPGet] attribute.
  • The method passes the model to the view which is a form that has the usual text boxes that bind to the various string properties and an img element that is bound to the byte array/image.

This all works as it should and I see all the data including the image. This is all pretty standard stuff.

But when the user submits the form to my [HTTPPost] version of the action method that accepts the same model as its parameter the image property is null. i.e. the image property does not appear to be part of the model binding.

In the normal course of events we would do some validation and pass the model back to the view to be rendered so the user can see if the edits were successfull or not. But just passing the model back "as is" - the view does not render the image again because its no longer in the model.

I know I can go and get the image again (from the database or where ever) and put it back in the model before passing it to the view, but is that the right stratergy or have I missed something?

Regards, Simon

A: 

How do you render the image that is contained as binary data in model? Do you use classic webforms controls (what would be not recomded in mvc terminology)? Anyway, if the image is only displayed in the view it is not posted when the user submits the form because only input fields (checkboxes, text fields, hiddens) are submited to the server. image element is not. Remember that in MVC it is simple HTML doing all the work of posting data to the server - there is no viewstate nor automatical postback that will persist the state of the controls. You have two solutions:

  1. Encode binary data in some hidden field so it would be posted again.
  2. (better) Do not send the image data back and forth between the client and the server, but detect if the user provided new image (i expect you wolud use file input for that) and if the user left the file input empty then update the model with the image already stored in database to display it again. Otherwise update the image in the database.

Anyway, i'm curious how do you display image from binary data in model. I think it would be simpler to create some controller action that would return binary data so you could use URL of that action in src attribute of img tag, or store images as files and use their URL instead of binary data.

PanJanek
Pan,Thanks for the clarification regarding image elements. It makes sense that the image data doesn't get posted back. When I looked more closely at my code I do was as you suggested using a controller action method as the source of the image tag. I should have looked at my code more closely before posting the question (embarassed). But thanks again for your reply.
Simon Lomax