向 MVC 控制器提交 JSON 数据。为什么它不处理复杂类型?
将 C# 帮助器类定义为:
`
公共类 SelectFiles { 公共字符串主机{获取;放;让
public List<SelectedFile> Files { get; set; }
}
public class SelectedFile
{
public string ShortName {get; set;}
public string Category { get; set; }
}`
我们定义两个控制器方法(+反序列化助手):
private SelectedFiles Deserialize(string json)
{
using (MemoryStream stream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SelectedFiles));
return (SelectedFiles) serializer.ReadObject(stream);
}
}
/// <summary>
/// MVC seems to be incapable to deserizalize complex types.
/// </summary>
/// <param name="selectedFilesJSON"></param>
/// <returns></returns>
public void UploadFiles(SelectedFiles selectedFilesJSON)
{
var result = selectedFilesJSON;
}
/// <summary>
/// </summary>
/// <param name="selectedFilesJSON"></param>
/// <returns></returns>
public void UploadFilesJSON(string selectedFilesJSON)
{
if (!string.IsNullOrEmpty(selectedFilesJSON))
{
var result = this.Deserialize(selectedFilesJSON);
}
}
使用 javascript 调用
var SelectedFiles = [];
var SelectedFile;
selectedCheckBoxes.each(function(idx, element){
SelectedFile = {ShortName: element.value, Category: "a category"};
SelectedFiles.push(SelectedFile);
});
var selectedFileMessage = {};
selectedFileMessage.Host = "test"
selectedFileMessage.Files = SelectedFiles;
var message = $.toJSON(selectedFileMessage);
$.ajax({url:"UploadFilesJSON", type:'POST', traditional:true, data: { selectedFilesJSON : message } , success: function(result){ alert(result);} });
$.ajax({url:"UploadFiles", type:'POST', traditional:true, data: { selectedFilesJSON : selectedFileMessage } , success: function(result){ alert(result);} });
第一个 .ajax POST 方法将起作用。基本上将序列化的 JSON 对象作为字符串转储到控制器方法。
第二个 .ajax POST 不起作用。它似乎没有弄清楚如何从参数列表中反序列化“SelectedFiles”帮助器类。
一个字:“目瞪口呆”
Define C# helper classes as:
`
public class SelectedFiles
{
public string Host { get; set; }
public List<SelectedFile> Files { get; set; }
}
public class SelectedFile
{
public string ShortName {get; set;}
public string Category { get; set; }
}`
And let define two controller methods (+ Deserialize helper):
private SelectedFiles Deserialize(string json)
{
using (MemoryStream stream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SelectedFiles));
return (SelectedFiles) serializer.ReadObject(stream);
}
}
/// <summary>
/// MVC seems to be incapable to deserizalize complex types.
/// </summary>
/// <param name="selectedFilesJSON"></param>
/// <returns></returns>
public void UploadFiles(SelectedFiles selectedFilesJSON)
{
var result = selectedFilesJSON;
}
/// <summary>
/// </summary>
/// <param name="selectedFilesJSON"></param>
/// <returns></returns>
public void UploadFilesJSON(string selectedFilesJSON)
{
if (!string.IsNullOrEmpty(selectedFilesJSON))
{
var result = this.Deserialize(selectedFilesJSON);
}
}
With javascript calling
var SelectedFiles = [];
var SelectedFile;
selectedCheckBoxes.each(function(idx, element){
SelectedFile = {ShortName: element.value, Category: "a category"};
SelectedFiles.push(SelectedFile);
});
var selectedFileMessage = {};
selectedFileMessage.Host = "test"
selectedFileMessage.Files = SelectedFiles;
var message = $.toJSON(selectedFileMessage);
$.ajax({url:"UploadFilesJSON", type:'POST', traditional:true, data: { selectedFilesJSON : message } , success: function(result){ alert(result);} });
$.ajax({url:"UploadFiles", type:'POST', traditional:true, data: { selectedFilesJSON : selectedFileMessage } , success: function(result){ alert(result);} });
The first .ajax POST method will work. Basically dumping the serialized JSON object as a string to the controller method.
The second .ajax POST does not work. It does not seems to figure out how to deserialize the 'SelectedFiles' helper class from the argument list.
One word: 'Flabergasted'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已使用 ASP.NET MVC 2 标记了您的问题,如果这是正确的,那么您没有开箱即用的 JSON 绑定支持。 JsonValueProviderFactory 自 ASP.NET MVC 3 起可用。要在早期版本的 ASP.NET MVC 中绑定 JSON,您需要创建值提供程序或模型绑定程序。您可以在这里阅读更多相关信息: 将 JSON 发送到 ASP.NET MVC 操作方法参数
You have tagged your question with ASP.NET MVC 2, if that is correct you don't have JSON binding support out of the box. JsonValueProviderFactory is available since ASP.NET MVC 3. To bind JSON in previous versions of ASP.NET MVC you need to create a value provider or model binder. You can read more about it here: Sending JSON to an ASP.NET MVC Action Method Argument
在第二种情况下,预期的发布数据是 SelectedFile 的集合。你尝试过吗
In the second case the expected post data is the collection of SelectedFile. Have you tried
您需要将您的 POST contentType 标记为 json。此外,MVC 会将模型绑定到您的模型,因此您不需要使用字符串作为操作参数。 Phil Haack 有一个 示例在他的博客上。
You need to mark your POSTs contentType as json. Also, MVC will modelbind to your model so you don't need to use a string as an action parameter. Phil Haack has an example on his blog.