如何从复杂的按键一的FormCollection打造C#对象 [英] How to build C# object from a FormCollection with complex keys
问题描述
我有一个JavaScript对象, OBJ
,获取传递通过一个$。员额(MVC的动作),像这样:
i have a javascript object, obj
, that gets passed to an mvc action via a $.post() like so:
var obj = {
Items: [{ Text: "", Value: { Property1: "", Property2: "" },
{ Text: "", Value: { Property1: "", Property2: "" }]
};
$.post('MyAction', obj, function() {});
动作的签名是这样的:
the action signature looks like this:
public ActionResult MyAction(FormCollection collection)
{
}
我需要能够建立从对象的的FormCollection
,但是我运行到一个问题,即键形式:
i need to be able to build an object from the FormCollection
, however i'm running into an issue where the keys are in the form:
"Items[0][Text]"
"Items[0][Value][Property1]"
"Items[0][Value][Property2]"
"Items[1][Text]"
"Items[1][Value][Property1]"
"Items[1][Value][Property2]"
我不知道是否有建立从所需的C#对象的干净的方式给定的的FormCollection
。我知道我可以改变操作方法签名参加对象我感兴趣的类型,但这是presenting其自身的问题。
i'm wondering if there's a clean way to build the desired C# object from the given FormCollection
. i understand that i could change the action method signature to take in the type of object i'm interested in, but that was presenting its own issues.
推荐答案
如果你可以让你的,而不是数据的JSON版本发送更改的JavaScript方面,我想你既可以接受一个字符串,并做了JSON反序列化自己的控制器或做样板粘结剂为你做它。
If you can change the javascript side so you're sending in a json version of the data instead, I would think you could either accept a string and do a json deserialization yourself in the controller or do a model binder to do it for you.
- HTTP://$c$c.google.com/p/jquery -json /
- http://stackoverflow.com/questions/2140845/asp-net-mvc-sending-json-to-controller
- http://api.jquery.com/serializeArray/
- http://code.google.com/p/jquery-json/
- http://stackoverflow.com/questions/2140845/asp-net-mvc-sending-json-to-controller
- http://api.jquery.com/serializeArray/
这篇关于如何从复杂的按键一的FormCollection打造C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!