MVC中,Ajax Post 数组的实现方案

1. HTMLPost 做Post的情况

如果HTML 脚本是这样的话:

<form action="P.aspx">
<input type="checkbox" name="v" value="v1"/>
<input type="checkbox" name="v" value="v2"/>
</form>

当Post 的时候, Model 可以如下定义进行接收:

public User
{
publicstring[] v {get ;set ;}
}

[HttpPost]
public ActionResult P(User user)
{
...
}

事实上, FORM POST 的时候,POST的数据是:  v=v1&v=v2 的样子.

2. Ajax Post 情况

用 Ajax Post 的时候, 由于Post 的是Json 数据, Json 数据的Key 唯一. v只能等于一个值.而如果用数组POST ,如:

$.post("/Account/Register", { "ck": ["c", "k"] }, function (res) {
alert(res);
});

POST数据时,系统会变成: 

ck[]  : c

ck[]  : k

3.分析jQuery

查看 jQuery , 在函数: buildParams处:

function buildParams( prefix, obj ) {
if ( jQuery.isArray(obj) ) {
// Serialize array item.
jQuery.each( obj, function( i, v ) {
if ( traditional ) {
// Treat each array item as a scalar.
add( prefix, v );
}
else {
// If array item is non-scalar (array or object), encode its
// numeric index to resolve deserialization ambiguity issues.
// Note that rack (as of 1.0.0) can't currently deserialize
// nested arrays properly, and attempting to do so may cause
// a server error. Possible fixes are to modify rack's
// deserialization algorithm or to provide an option or flag
// to force array serialization to be shallow.
buildParams( prefix +"["+ ( typeof v ==="object"|| jQuery.isArray(v) ? i : "" ) +"]", v );
}
});

}
elseif ( !traditional && obj !=null&&typeof obj ==="object" ) {
// Serialize object item.
jQuery.each( obj, function( k, v ) {
buildParams( prefix
+"["+ k +"]", v );
});

}
else {
// Serialize scalar item.
add( prefix, obj );
}
}

我想是这样的:

如果把数组POST 成这要: ck=c&ck=k 那如果ck[0] 还是数组, 事情就会变得很麻烦 . 它会 ck=c&ck=k 有多种意思:

1.  ["c","k"]

2.  [["c"],["k"]]

3.  [["c","k"]]

所以 jQuery  这样写是有道理的.

4.解决方案

为了欺骗  type v  , 把传递的数组对象转换为 Json 对象,Json对象的Key是数组Index。

//第一种方式。
 
var data = Object();
data[
0] ="c" ;
data[
1] ="k" ;

//上述写法不是一个数组,而是一个字典, Key 是Int , 让他绑定: prefix + "[" + k + "]" .也可以写为:
//第二种方式:

var data = Object();

data[
"0"] ="c" ;

data[
"1"] ="k" ;


简写为:

$.post(
"/Account/Register", { ck : {0: "c" , 1 : "k" }}, function (res) {
alert(res);
});

 

MVC能够接受的两种数组方式:
 
1.     ck=c&ck=k
2.     ck[0]=c&ck[1]=k

5.绑定复合对象

如绑定到如下Model上:

    /// <summary>
/// 自定义人员模型
/// </summary>
public class PersonModel
{
public PersonRule.Entity Person { get; set; }
/// <summary>
/// 部门,用于上传接收值.
/// </summary>
public string Dept { get; set; }

/// <summary>
/// 角色,用于上传接收值.
/// </summary>
public string Role { get; set; }

public PersonModel()
{
Person = new PersonRule.Entity();
}

}

客户端需要Post 如下Json:

Dept.BusType:4
Dept.MySkin:
Dept.Langs:Zh
Dept.DefaultLang:Zh
Dept.Id:1
Dept.WebName:Site
Dept.Phone:
Dept.KeyWords:
Dept.GisPos:
Dept.Name:系统管理组
Dept.Address:
Dept.ReMark:系统管理组
Dept.EndTime:9999/1/1 0:00:00
Dept.SortID:10

即属性前要有前缀且以 "." 连接。  DefaultModelBinder 相当强大。

------------------------------------------------------------------------------------------------------------

后记:如果服务器Model 是

public User
{
publicstring[] names {get;set;}
}

public Dept
{
public User[] users {get ;set ;}
}

Dept作为Model , 客户端该如何提交呢. 这就需要用 ModelBinder 了 . 是否有机制实现从JS 到MODEL 的自动转换, 就不得而知了, 有知道的朋友告知一下.:)

alarm   作者:NewSea     出处:http://newsea.cnblogs.com/    QQ,MSN:iamnewsea@hotmail.com

  如无特别标记说明,均为NewSea原创,版权私有,翻载必纠。欢迎交流,转载,但要在页面明显位置给出原文连接。谢谢。
原文地址:https://www.cnblogs.com/newsea/p/2007384.html