数据编码和解码MVC喜欢的方式 [英] Encoding and decoding data the way MVC likes it

查看:111
本文介绍了数据编码和解码MVC喜欢的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我c将其使用得到MVC和连接$ C $的数据 HttpUtility.HtmlEn code 。问题是,MVC是不喜欢包含符号(&安培;)的形式值的象征。例如,这...

I get data from MVC and encode it using HttpUtility.HtmlEncode. The problem is that MVC is not fond of form values containing the ampersand (&) symbol. For example, this...

HttpUtility.HtmlEncode("'");

...结果&放大器;#39; 。我把它拿来一个 GET ,但如果我提交这份上 POST ,我看到下面的动作错误:

...results in '. I fetch it on a GET, but if I submit this on a POST, I see the following action error:

Exception = {"A potentially dangerous Request.Form value was detected from the client (selection=\"'...\")."}

我怎样才能连接code我在 GET 的方式,使MVC在 POST高兴数据?我不能属性设置为 [AllowHtml] ,因为模型是一个第三方DLL一个单独的类中,我没有控制权,也不是我能重新实现,它似乎。

How can I encode my data on a GET in a way that makes MVC happy on a POST? I cannot set the property to [AllowHtml] because the model is within a separate class in a 3rd party DLL that I do not have control over, nor can I re-implement it, it seems.

推荐答案

我发现了序列化/反序列化的一个更好的办法,我想和大家一起分享。由于我使用JavaScript和 DataTables.NET 做AJAX POST请求,我可以使用 System.Web.Helpers 方法=htt​​ps://msdn.microsoft.com/en-us/library/system.web.helpers.json.en$c$c( v = vs.111)的.aspx相对=nofollow> Json.En code 并的 Json.De code ,因为它们直接转换成JSON(JavaScript对象符号)和背部,它似乎从JavaScript和高达MVC工作完美我的需要,我应该使用它始终。

I found a better way of serializing/deserializing and I wanted to share it with everyone. Since I am using JavaScript and DataTables.NET to do AJAX POST requests, I was able to use the System.Web.Helpers methods called Json.Encode and Json.Decode, since they convert directly to JSON (JavaScript Object Notation) and back, it seems to work perfectly from JavaScript and up to MVC for my needs, and I should have used it all along.

这篇关于数据编码和解码MVC喜欢的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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