为什么.NET核心HTTP清除我的字节数组 [英] Why is .NET core HTTP post clearing my byte array

查看:52
本文介绍了为什么.NET核心HTTP清除我的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET Core HTTP Post清除我的字节数组的问题。



我可以选择一个图像,并在控制器中捕获它,保存它到数据库然后在下次加载表单时检索它成功呈现。



然后开始玩乐。如果我单击表单上的保存而不更改任何内容,则在打开标记为[HttpPost]的控制器时,检查表单模型所基于的类,除了我的字节数组和ImageMimeType属性之外,类中的所有内容都保持不变。 br />


他们都被空白了。任何想法为什么会这样?

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;





我的尝试:



大量的在线搜索以及javascript中的解决方法,以获取渲染图像并将其传递回Html元素。到目前为止没有喜悦。



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.

推荐答案

你需要一个隐藏的字段,其名称与你试图保持的属性相匹配。



对于MIME类型,这很简单:

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:

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)


这篇关于为什么.NET核心HTTP清除我的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆