2011-03-01 33 views
1

我的問題在於如何發送列表值到控制器上的ajax帖子。用我的實際代碼,這個列表在發佈後是空的。如何使用ajax發佈列表

我的觀點:

@using (Html.BeginForm("UsersList", "Project", FormMethod.Post, new { id = "Users" })) 
{ 
    <div id="MyList" > 
     <table> 
     <thead> 
     <tr><th></th><th>User</th><th>End date</th></tr> 
     </thead> 

    @Html.EditorFor(x => x.GetUsers) 
     </table> 
      </div> 

     <script type="text/javascript"> 
     $(function() { 
      $('#MyList).dialog({ 
       autoOpen: false, 
       width: 820, 
       buttons: { 
        "Save": function() { 
         $("#Users").submit(); 
        }, 

        "Cancel": function() { 
         $(this).dialog("close"); 
        } 
       } 
      }); 
    </script> 

型號:

public class ProjectModel 
    { 

     public List<ProjectList>GetUsers{get;set;} 

     } 

    public class ProjectList 
    { 
     public bool Selected { get; set; } 
     public int UserID { get; set; } 

} 

我的控制器:

[HttpPost] 
     public ActionResult UsersList(ProjectModel model) 
     { 
      return View(model); 
     } 

回答

0

發送列表可就難了。我認爲MVC 3.0 has (in-built) support for sending JSON to the controller這將使這更容易。

但是與之前的版本,我認爲這篇文章應該是你找什麼: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

切實你傾其所有在列表中的名稱相同的HTML(如名稱會是怎樣獲取的關鍵發送。/值字符串被髮送,這將作爲綁定列表

對於更復雜的項目,你命名每一個對象的屬性,但添加一個索引顯示反對他們得到重視過:

<form method="post" action="/Home/Create"> 

<input type="text" name="[0].Title" value="Curious George" /> 
<input type="text" name="[0].Author" value="H.A. Rey" /> 
<input type="text" name="[0].DatePublished" value="2/23/1973" /> 

<input type="text" name="[1].Title" value="Code Complete" /> 
<input type="text" name="[1].Author" value="Steve McConnell" /> 
<input type="text" name="[1].DatePublished" value="6/9/2004" /> 

<input type="text" name="[2].Title" value="The Two Towers" /> 
<input type="text" name="[2].Author" value="JRR Tolkien" /> 
<input type="text" name="[2].DatePublished" value="6/1/2005" /> 

<input type="submit" />