代码之家  ›  专栏  ›  技术社区  ›  user171910

在ASP.NETMVC中,如何将对象列表传递给[Post]控制器参数?

  •  0
  • user171910  · 技术社区  · 16 年前

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SendMail(List<Student> students)
    {
        .............
        return View("Success",students)
    }
    

    我用Ajax.ActionLink尝试了这一点:

    <%= Ajax.ActionLink("Send Mail to list AT SCS", "SendMail", students = Model,
                                 new AjaxOptions()
                                 {
                                     Confirm = "Are you sure you want to send mails?",
                                     HttpMethod = "POST"
                                 }
                         )%>
    

    但是当我在VS中使用调试器时,我看到列表是空的。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Misha N.    16 年前

    这个表单应该是int(例如,我假设学生有Email和Name属性):

    “前缀[“+迭代器+”].PropertyName”

     <ul>
        <% int i = 0; foreach (Student s in (IEnumerable)this.Model)
           {%>
        <li>
            <%=Html.TextBox("student[" + i + "].Email")%>
            <%=Html.TextBox("student[" + i + "].Name")%>
        </li>
        <%i++;
           } %>
    </ul>
    

    如果命名方式不同,则可以在操作时设置前缀:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SendMail(Bind(Prefix="thePrefix")]List<Student> students)
    {
        .............
        return View("Success",students)
    }