asp.net mvc 后台怎么接受前端返回的array list dictionary

参考了别人的文章,我这样尝试去写:

数据源:memberInRoles

1 var memberInRoles= {};
2             for(var i=0;i<sureOptions.length;i++){
3                 memberInRoles["memberInRoles["+i+"].UserId"]=sureOptions[i].value;
4                 memberInRoles["memberInRoles["+i+"].RoleId"]="@Model.RoleId";
5             }

Controller Action

1  [HttpPost]
2         public JsonResult UserSelectSave(List<MemberInRole> memberInRoles)
3         {
4             return Json(new { data = 0, msg = "" });
5         }

尝试使用$.ajax

 1             $.ajax({
 2                 type: "POST",
 3                 dataType: "json",
 4                 url: "@Url.Action("UserSelectSave", "RoleManager", new { area = "DeskTop" })",
 5                 data: { memberInRoles:memberInRoles },
 6                 beforeSend: function () { },
 7                 complete: function () { },
 8                 success: function (data) {
 9                     // format error
10                     if(!data||(!data.status&&data.status!=0)){
11                         alertBox(boxcallback, "0", "提示", "系统参数错误!");
12                         return false;
13                     }
14                     // success
15                     if (data.status == "0") {
16                         
17                     } else {
18                         alertBox(boxcallback, "0", "提示", data.msg);
19                         return false;
20                     }
21                 },
22                 error: function (data) {
23                     alertBox(boxcallback, "0", "提示", "系统异常,请稍后重试");
24                     return false;
25                 }
26             });

结果发现接收到的参数为null,继续修改:

 

$.ajax({
    type: "POST",
    dataType: "json",
    url: "@Url.Action("UserSelectSave", "RoleManager", new { area = "DeskTop" })",
    data: { memberInRoles},
    beforeSend: function () { },
    complete: function () { },
    success: function (data) {
        // format error
        if(!data||(!data.status&&data.status!=0)){
            alertBox(boxcallback, "0", "提示", "系统参数错误!");
            return false;
        }
        // success
        if (data.status == "0") {
            
        } else {
            alertBox(boxcallback, "0", "提示", data.msg);
            return false;
        }
    },
    error: function (data) {
        alertBox(boxcallback, "0", "提示", "系统异常,请稍后重试");
        return false;
    }
});

结果直接报黄页,刷洗的地址是提交之前的地址,而且把我的参数名称前都加了一个hd字符。

对于“DXWorkFlow.Web.Areas.Desktop.Controllers.RoleManagerController”中方法“System.Web.Mvc.ActionResult
UserManager(Int32, Int32, System.String, System.String)”的不可以为 null
的类型“System.Int32”的参数“parentId”,参数字典包含一个 null 项。可选参数必须为引用类型、可以为 null
的类型或声明为可选参数。
Parameter name: parameters

 

 看别人提交时貌似都是采用$.post方式提交的。

结果使用$.post确可行,实在不明白什么原因。

 1 $.post("@Url.Action("UserSelectSave", "RoleManager", new { area = "DeskTop" })",
 2     memberInRoles,
 3     function(data){
 4         // format error
 5         if(!data||(!data.status&&data.status!=0)){
 6             alertBox(boxcallback, "0", "提示", "系统参数错误!");
 7             return false;
 8         }
 9         // success
10         if (data.status == "0") {
11     
12         } else {
13             alertBox(boxcallback, "0", "提示", data.msg);
14             return false;
15         }
16     }
17 );

实在想不明白$.ajax与$.post方式有什么区别。

如果哪位知道$.ajax与$.post区别具体信息,请赐教。

 

参考文章:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

http://www.cnblogs.com/birdwudi/archive/2010/08/26/1809511.html

posted @ 2015-11-28 03:38  cctext  阅读(2211)  评论(0编辑  收藏  举报