通过JSON从表单-ASP.Net MVC2发布一个对象数组

时间:2013-10-31 16:50:03

标签: c# .net asp.net-mvc-2

我尝试在发布表单时发布一个对象数组,但我总是在控制器中找到一个空列表

的JavaScript

function defineuser(userList) {
   var user = null;
var userAppModuleItemList = new Array();

    for (var i = 0; i < userList.length; i++){
        values = userList[i].split(",");
        user = {
            ID: values[0],
            Name: values[1],
            LastName: values[2]
        };
     userAppModuleItemList.push(user );
    }

    $("#userList").val(userAppModuleItemList);
};

查看

<%using (Html.BeginForm("UpdateUser", "User", FormMethod.Post, new { id = "userForm" }))
  {%>
<%=Html.Hidden("userList")%>
...
<%}%>

位指示

[HttpPost]
public JsonResult UpdateUser(List<UserDTO> userList)
{
    // The userList is null
}

用户类定义

public class UserDTO
{
    public int ID { get; set; }
    public string Name { get; set;}
    public string LastName { get; set; } 
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试traditional : true发布数组对象

$.ajax({ 
      type: "POST",
      url: "<%= Url.Action("MyAction","Controller")%>",
      datatype: "json",
      traditional: true,
      data: { userList : arrayOfValues }              
 });