2011-07-19 47 views
24

我正在开发一个MVC3应用程序。我的客户端ViewModel包含一个SQL Server RowVersion属性,它是一个字节[]。它在客户端呈现为Object数组。当我尝试将我的视图模型发布到控制器时,RowVersion属性始终为空。MVC3 - 将字节数组发布到控制器 - 数据库RowVersion

我假设控制器序列化程序(JsonValueProviderFactory)忽略对象数组属性。

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

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

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

然后我后视图模型到控制器,像这样:

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); 
     } 
    }); 

这是我在控制器动作:

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

更新:根据Darin的回答。

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

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); 
    } 
    } 
+0

嘿,你可以发表您的控制器动作的代码? – Tocco

+0

嗨@Darin Dimitrov,你可以添加'serialization'标签吗? – Tocco

+0

@Tocco,我添加了它。 –

回答

36

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

使它这样而不是byte[]您的视图模型包含string属性,它是的base64表示形式。然后,您将不会有任何问题将其转移到客户端,然后返回到服务器,您将能够从Base64字符串中获取原始byte[]

+0

+1旧的帖子,但绝对解决了我今天的问题。 – Nope

+1

'Convert.ToBase64String(byte [])' –

+0

非常好的解决方案(+1)! – Christos

1

Json.NET自动将字节数组编码为Base64。

您可以使用JsonNetResult代替JsonResult

https://gist.github.com/DavidDeSloovere/5689824

using System; 
using System.Web; 
using System.Web.Mvc; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Serialization; 

public class JsonNetResult : JsonResult 
{ 
    public override void ExecuteResult(ControllerContext context) 
    { 
     if (context == null) 
     { 
      throw new ArgumentNullException("context"); 
     } 

     var response = context.HttpContext.Response; 

     response.ContentType = !string.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/json"; 

     if (this.ContentEncoding != null) 
     { 
      response.ContentEncoding = this.ContentEncoding; 
     } 

     if (this.Data == null) 
     { 
      return; 
     } 

     var jsonSerializerSettings = new JsonSerializerSettings(); 
     jsonSerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat; 
     jsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
     var formatting = HttpContext.Current != null && HttpContext.Current.IsDebuggingEnabled ? Formatting.Indented : Formatting.None; 
     var serializedObject = JsonConvert.SerializeObject(Data, formatting, jsonSerializerSettings); 
     response.Write(serializedObject); 
    } 
} 

用法:

[HttpPost] 
public JsonResult Save(Contact contact) { 
    return new JsonNetResult { Data = _contactService.Save(contact) }; 
} 
相关问题