Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem with a .NET Core HTTP Post clearing my byte array.

I can choose an image, and capture it in the controller, save it to the database then retrieve render it successfully the next time the form is loaded.

Then the fun starts. If I click save on the form without changing anything an examine the class that the form Model is base on as soon as the controller tagged with [HttpPost] is opened everything on the class is unchanged as expected except for my byte array and ImageMimeType Properties

They have both been blanked. Any ideas why this might be?
public Byte[] p_Photo
{
    get
    {
        return m_Photo;
    }
    set
    {
        m_Photo = value;
        m_FlagSettings = m_FlagSettings | m_FlagBits.PHOTO;
    }

}
[Column("ImageMimeType")]
public string p_ImageMimeType {
    get
    {
        return m_ImageMimeType;
    }
    set
    {
        m_ImageMimeType = value;
    }
}
protected string m_ImageMimeType;
protected Byte[] m_Photo;


What I have tried:

Lots of searching on line and also a workaround in javascript to take the rendered image and pass it back to the Html Element. No joy so far.
Posted
Updated 24-Oct-18 8:54am

1 solution

You'll need a hidden field with a name that matches the property you're trying to persist.

For the MIME type, that's simple:
@Html.HiddenFor(m => m.p_ImageMimeType)

For the image bytes, it's not quite as simple, since byte arrays aren't supported by default. You could write a custom model binder; but it's probably simpler to use a separate property:
C#
public string p_PhotoData
{
    get { return m_Photo == null ? null : Convert.ToBase64String(m_Photo); }
    set { m_Photo = string.IsNullOrEmpty(value) ? null : Convert.FromBase64String(value); }
}
@Html.HiddenFor(m => m.p_PhotoData)
 
Share this answer
 
Comments
Ger Hayden 24-Oct-18 16:46pm    
Thanks Richard, I was about to write that I already have the @Html.Hidden for all properties including those two when that little voice suggested checking the cshtml first. They are not there! Interesting use of the property, I'll add them and try your suggestion on the property in the morning and report back.
Ger Hayden 25-Oct-18 1:08am    
It was the missing Hiddens. Much appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900