ASP.NET MVC:如何保持一个字段字节[] [英] ASP.NET MVC: How do I keep a field byte[]
问题描述
我有一个字段,其类型为byte []。此字段将握住我的实体的财产RecordVersion(数据库中的时间戳)。如何保持这一领域,这样,当我救我的实体它是可用?
I've got a field which its type is byte[]. This field will hold my entity's RecordVersion property (timestamp in the database). How do I keep this field so that when I save my entity it is available?
我试过两个不同的东西,至今都没有成功:
这使得System.Byte []
I've tried two different things and haven't succeeded so far: This renders "System.Byte[]":
<%= Html.Hidden("RecordVersion", Model.RecordVersion.ToString()) %>
这将引发ModelStateError其中类型不能转换:
This throws a ModelStateError where the type couldn't be converted:
ViewData["RecordVersion"] = entity.RecordVersion
显然,默认MVC的机制,它绑定/解除绑定不喜欢多字节[]字段.....
Apparently the default MVC's mechanism that does the bind/unbind doesn't like much byte[] fields .....
推荐答案
您需要做一个模型绑定器,并进行注册。
You need to make a modelbinder and register it.
这<一个href=\"http://stephenwalther.com/blog/archive/2009/02/25/asp.net-mvc-tip-49-use-the-linqbinarymodelbinder-in-your.aspx\"相对=nofollow>文章展示了如何在一个隐藏字段使用时间戳从LINQ的数据库就像你在做什么。
This article shows how to use a timestamp from a linq database in a hidden field much like what you are doing.
ModelBinders.Binders.Add(typeof(Binary), new LinqBinaryModelBinder());
在Global.asax中进行注册。
In global.asax to register it.
这LinqBinaryModelBinder是在期货大会。如果你想用户字节[]你必须自己写一个。
That LinqBinaryModelBinder is in the futures assembly. If you want to user byte[] you'll have to write one yourself.
这篇关于ASP.NET MVC:如何保持一个字段字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!