MVC3 - 发帖字节数组控制器 - 数据库RowVersion [英] MVC3 - posting byte array to a controller - Database RowVersion

查看:185
本文介绍了MVC3 - 发帖字节数组控制器 - 数据库RowVersion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个MVC3应用程序。我的客户端视图模型包含一个SQL Server RowVersion属性,这是一个byte []。它呈现为在客户端对象数组。当我尝试我的视图模型张贴到控制器,RowVersion属性总是空。

我假设控制器的串行(JsonValueProviderFactory)被忽略的对象数组属性。

我看到这个博客,但是这并不适用,因为我张贴的JSON,而不是形式的标记:
<一href=\"http://thedatafarm.com/blog/data-access/round-tripping-a-timestamp-field-with-ef4-1-$c$c-first-and-mvc-3/\">http://thedatafarm.com/blog/data-access/round-tripping-a-timestamp-field-with-ef4-1-$c$c-first-and-mvc-3/

我的观点呈现我的视图模型像这样:

 &LT;脚本类型=文/ JavaScript的&GT;
  VAR视图模型= @ Html.Raw(Json.En code(this.Model));
&LT; / SCRIPT&GT;

然后我张贴视图模型来像这样的控制器:

  VAR数据= {
        接触:视图模型
    };    $阿贾克斯({
        输入:POST,
        网址:'/联系人/保存,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据:JSON.stringify(数据),
        数据类型:JSON,
        成功:功能(数据){
            //成功
        },
        错误:功能(XMLHtt prequest,textStatus,errorThrown){
            警报(XMLHtt prequest.responseText);
        }
    });

下面是我在控制器动作:

  [HttpPost]
  公共JsonResult保存(联系方式联系我们){
     返回this.Json(this._contactService.Save(接触));
  }

更新:根据达林的答案

我希望的是清洁的解决方案,但由于达林提供唯一的答案,我将不得不添加将我的序列化字节[]row_version属性到Base64字符串的自定义属性。当的Base64字符串设置为新的自定义属性,它的字符串转换回一个字节[]。下面是我添加到我的模型定制RowVersion属性:

 字节公众[] {row_version
     得到;
     组;
  }  公共字符串RowVersion {
     获得{        如果(this.row_version!= NULL)
           返回Convert.ToBase64String(this.row_version);        返回的String.Empty;
     }
     集合{        如果(string.IsNullOrEmpty(值))
           this.row_version = NULL;
        其他
           this.row_version = Convert.FromBase64String(值);
     }
  }


解决方案

  

我的客户端视图模型包含一个SQL Server RowVersion属性,这是一个byte []


让这个而不是这是一个字节[] 您的视图模型包含字符串属性< A HREF =htt​​p://en.wikipedia.org/wiki/Base64>的base64 重新此<$ C $的C presentation>字节[] 。然后,你将不会有它往返于客户端和回服务器,你就可以得到原来的任何问题字节[] 从Base64编码字符串。

I am working on an MVC3 application. My client side ViewModel contains a SQL Server RowVersion property, which is a byte[]. It is rendered as an Object array on the client side. When I attempt to post my view model to a controller, the RowVersion property is always null.

I am assuming that the Controller serializer (JsonValueProviderFactory) is ignoring the Object array property.

I have seen this blog, however this does not apply, as I am posting JSON and not the form markup: http://thedatafarm.com/blog/data-access/round-tripping-a-timestamp-field-with-ef4-1-code-first-and-mvc-3/

My view renders my viewmodel like so:

<script type="text/javascript">
  var viewModel = @Html.Raw( Json.Encode( this.Model ) );
</script>

I then post the viewModel to the controller like so:

    var data = {
        'contact': viewModel
    };

    $.ajax({
        type: 'POST',
        url: '/Contact/Save',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(data),
        dataType: 'json',
        success: function (data) {
            // Success
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.responseText);
        }
    });

Here is my action in the controller:

  [HttpPost]
  public JsonResult Save(Contact contact) {
     return this.Json( this._contactService.Save( contact ) );
  }

UPDATE: based on Darin's answer.

I was hoping for a cleaner solution, but since Darin provided the only answer, I will have to add a custom property that will serialize my byte[] "row_version" property to a Base64 string. And when the Base64 string is set to the new custom property, it converts the string back to a byte[]. Below is the custom "RowVersion" property that I added to my model:

  public byte[] row_version {
     get;
     set;
  }

  public string RowVersion {
     get {

        if( this.row_version != null )
           return Convert.ToBase64String( this.row_version );

        return string.Empty;
     }
     set {

        if( string.IsNullOrEmpty( value ) )
           this.row_version = null;
        else
           this.row_version = Convert.FromBase64String( value );
     }
  }

解决方案

My client side ViewModel contains a SQL Server RowVersion property, which is a byte[]

Make it so that instead of a byte[] your view model contains a string property which is the base64 representation of this byte[]. Then you won't have any problems roundtripping it to the client and back to the server where you will be able to get the original byte[] from the Base64 string.

这篇关于MVC3 - 发帖字节数组控制器 - 数据库RowVersion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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