开发者

How to pass a list of objects to a [Post]controller parameter, in ASP.Net MVC?

开发者 https://www.devze.com 2022-12-09 16:46 出处:网络
My view page has a Model which is represented by a List of Students. I want to pass that Model to a controller parameter:

My view page has a Model which is represented by a List of Students.

I want to pass that Model to a controller parameter:

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

I tried this with an 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"
                             }
         开发者_StackOverflow            )%>

But when I use the debugger in VS, I see that the List is empty. Is not possible to send a list to a controller from a view? If not, how do I solve this?


How have you named the input's controls inside of the form you are posting?

It should be int this form (I assume that student has Email and Name property for example):

"PREFIX["+iterator+"].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>

if you named it differently then Prefix can be set on Action:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号