简体   繁体   English

在ASP.NET MVC控制器中接收嵌套的匿名对象

[英]Receive nested anonymous objects in ASP.NET MVC controller

I need a ASP.NET MVC controller, which receives anonymous object from JS in JSON to iterate thru its properties. 我需要一个ASP.NET MVC控制器,该控制器从JSON中的JS接收匿名对象以迭代其属性。 I used to do this, receiving Dictionary<string, object> . 我曾经这样做,接收Dictionary<string, object> But now one of values is Array , and insted of 但是现在值之一是Array ,并且由

receivedDictionary[Photos] = [object, object, object]

it gets it as 它得到它

receivedDictionary[Photos[0]] = object, receivedDictionary[Photos[1]] = object, receivedDictionary[Photos[2]] = object

I get not one dictionary entry with key = Photos and value = array, but many entries with key = Photos[x] and value = object. 我没有一个键= Photos和value = array的字典条目,但是键= Photos [x]和value = object的条目很多。

How do I get it as one entry in dictionary or is there any better way to get it as dynamic anonymous object and iterate thru its properties just like in JS? 如何将其作为字典中的一个条目获取?或者有更好的方法将其作为动态匿名对象并像JS中那样遍历其属性吗?

UPD: JSON looks like this: UPD:JSON如下所示:

{"fields":{"TotalFloors":"9","HouseNumber":"10","Photos":[{"id":0,"ParentID":0,"OriginalUrl":"py4s1y3uyqu","OriginalExt":".jpg","ThumbUrl":"2hn04w2lzuu","FormatUrls":{"WH_109_82_Url":"4cwjarqudvo","WH_766_454_Url":"oofm5qo21rr"}},{"id":0,"ParentID":0,"OriginalUrl":"t3csgq20iro","OriginalExt":".jpg","ThumbUrl":"j1uwwburmse","FormatUrls":{"WH_109_82_Url":"gm4qoery1u2","WH_766_454_Url":"a3c20re3g1d"}}],"Details":"Other details"}}

Controller definition: 控制器定义:

        [HttpPut]
        public ActionResult restId(string className, int id, Dictionary<string, object> fields)
        {
            ....
        }

The JsonValueProvider used by the DefaultModelBinder seems to be treating array in an odd fashion in this case (based on the source here ), so even a dynamic will most likely have the issue. 在这种情况下, DefaultModelBinder使用的JsonValueProvider似乎在以奇怪的方式处理数组(基于此处的源),因此即使是dynamic也很可能会出现此问题。 (Don't think you'll have this issue in MVC 6 however) (但是,不要认为您会在MVC 6中遇到此问题)

However, calling the JavascriptSerializer directly (which funny enough is what the default provider uses) produces the results you're after: 但是,直接调用JavascriptSerializer (足够有趣的是默认提供程序使用的)会产生您想要的结果:

var js = new JavaScriptSerializer();
var res = js.DeserializeObject(@"{'TotalFloors':'9','HouseNumber':'10','Photos':[...],'Details':'Other details'}");

To address your issue you could either alter the parameter to a string and run the above code in your action (obviously replacing the JSON string with the parameter), just means the JSON you're submitting from the front end would need to look more like: 为了解决您的问题,您可以将参数更改为字符串,然后在操作中运行上面的代码(显然用参数替换了JSON字符串),这仅意味着您要从前端提交的JSON需要看起来更像:

// wrapping the JSON object in quotes to stringify it
{ 'fields' : "{ 'TotalFloors': '9', 'HouseNumber': .... }" } 

Otherwise you could implement a custom JsonValueProvider like the one proposed here: https://json.codeplex.com/discussions/347099 否则,您可以实现自定义JsonValueProvider如此处建议的那样: https : //json.codeplex.com/discussions/347099

The custom value provider is probably the cleaner solution. 定制价值提供者可能是更清洁的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM