0

我有一个从前端调用的 MVC JSON 控制器方法。它看起来像这样:

public JsonResult FacetedSearch(string searchString, List<KeyValuePair<string,string>> facets)

我在前端通过 jQuery ajax 调用它,我以以下方式序列化数据:

JSON.stringify({searchString: "the matrix", facets: [{Key: "TypeName", Value: "Feature Film"}, {Key:"TypeName", Value:"Series"}]}

当我通过我的应用程序代码进行调试时,我看到 searchString 已成功传递给 MVC 方法,但该变量facets为我提供了 2 个 KeyValuePair 的列表,其中 Key 和 Value 为空。

我查看了我的序列化,它似乎是有效的,但无论出于何种原因,它都没有正确地转到我的应用程序。是什么赋予了?

4

2 回答 2

1

Rather than expect two objects in your signature, it would make more sense to expect a single object that contains both of your parameters. This would be something like the following.

public JsonResult FacetedSearch(RequestObject requestObject)
{ }

public class RequestObject
{
    public string searchString { get; set; } 
    public List<KeyValuePair<string,string>> facets { get; set; }
}

This way, when you send your JSON object, the signature is an object with two properties, just like the object that you are sending.

于 2013-03-19T18:25:04.430 回答
0

Per .NET 中是否有可序列化的通用键/值对类?

我发现了为什么它没有序列化它。显然它是不可序列化的。

于 2013-03-19T21:13:13.930 回答