Original address: http://www.cnblogs.com/fannyatg/archive/2012/04/16/2451611.html
--------------------------------------------------------------------------------------------------------------- ---
ASP. NET MVC Controller JSON array receives array dictionary type complex Object
Method One, (the most complex method)
ExtendedModelbinderCompletely customize the parsing method of a parameter.
<summary>
Pattern-bound array receiver eg:, receive character array: parameter markers such as [Modelbinder (typeof (Arraybind<string>))]string[] Arrayvalue
</summary>
<typeparam name= "T" ></typeparam>
Public classarraybind<t>: Imodelbinder
{
private_t getvalue<_t> (Modelbindingcontext bindingcontext,stringkey)
{
Valueproviderresult Valueresult = BindingContext.ValueProvider.GetValue (key);
BindingContext.ModelState.SetModelValue (key, Valueresult);
Return (_t) Valueresult.convertto (typeof (_t));
}
#regionIModelBinder Members
Public Objectbindmodel (ControllerContext controllercontext, Modelbindingcontext BindingContext)
{
Dictionary<int, t> list =newdictionary<int, t> ();
string[] Allkey = ControllerContext.HttpContext.Request.Form.AllKeys;
foreach (Stringkeyinallkey)
{
if (!key. StartsWith (Bindingcontext.modelname, Stringcomparison.currentcultureignorecase))
{
Continue
}
string[] _key = key. Split ('. ');
if (_key. Length!=2)
{
Continue
}
Intme = 0;
if (int. TryParse (_key[1],outme))
{
List. Add (Me, getvalue<t> (BindingContext, key));
}
}
var sort = from Kinlist by K.key ascending select K;
Returnsort. Select<keyvaluepair<int, T>, t> (C = c.value). ToArray ();
}
#endregion
}
It is then tagged in the controller's action method parameter, such as:
[HttpPost]
Publicactionresult Edit ([Bind (Prefix = "Leveldescription")][modelbinder (typeof (Arraybind<string>))]string[] Levelorder)
{
Returnjson (1);
}
Method two, URL parameter parsing method
Http://www.domain.com/post?Levelorder=1&Levelorder=2&Levelorder=3&Levelorder=4&Levelorder=5
This method is not recommended, URL length is limited, when you need to passa 404 error occurs when the URL passes a large amount of data, very scary oh. This "bug" is more subtle.
Method Three,
receiving arrays of complex objects, JS asynchronously submits the data,
var _lev = {};
_lev["Lev[0". Status "] =" 5 ";//The name must be the same from the zero start
_lev["Lev[1". Status "] =" 6 ";
$.post ("Edit", _lev
, function (JSON) {
if (json = = "1") {alert (' Success ')}
}, "JSON");
Synchronous commit Similarly, you need to construct such a structure in the form
<input type= "text" Name= "lev[0]. Status "value=" 5 "/><input type=" text "Name=" lev[1]. Status "value=" 6 "/> here will follow Parametername[index]. The PropertyName rule to parse the attribute. Where the index must be a sequential, positive integer starting at 0.
The action method, such as:
[HttpPost]
Publicactionresult Edit (level[] lev)
{
Returnjson (Lev);
}
If you are in action, you want toreceives a dictionary type value, it becomes more convenient,
$ ("Tr[name = ' Leelarea ']"). each (function (index) {
_lev["levelorder1[" + Index + "].Key"] = index;//here must start from scratch the name must be the same
_lev["levelorder1[" + Index + "].value"] = $ (this). Find (" textarea "). Val ();
});
if ($ (this). Valid ()) {
$.post ("Edit", _lev
, function (JSON) {
Alert (JSON);
}, "JSON");
}
The way to parse key here is to look for Parametername[index]. The structure of key, the way to parse value, is to find Parametername[index]. A structure such as value. If key or value is a complex type (such as the information type in the example above), then Parametername[index]. The key or Parametername[index].value will be considered a prefix (and can also be understood as a type). PropertyName is considered a suffix (that is, a property). The index is also required to be an uninterrupted positive integer starting with 0. Otherwise the part that is disconnected will not be parsed.
The action method, such as:
[HttpPost]
Publicactionresult Edit (dictionary<int,string> levelorder)
{
Returnjson (1);
}
. NET MVC Controller receives json/post way array dictionary type complex object