Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I Bind a Collection (IEnumerable) of a custom type?
    primarykey
    data
    text
    <p>I have the following Action to display a form with 3 items :</p> <pre><code>[HttpGet] public ActionResult ReferAFriend() { List&lt;ReferAFriendModel&gt; friends = new List&lt;ReferAFriendModel&gt;(); ReferAFriendModel f1 = new ReferAFriendModel(); ReferAFriendModel f2 = new ReferAFriendModel(); ReferAFriendModel f3 = new ReferAFriendModel(); friends.Add(f1); friends.Add(f2); friends.Add(f3); return View(friends); } </code></pre> <p>and then a Post action</p> <pre><code>[HttpPost] public ActionResult ReferAFriend(IEnumerable&lt;ReferAFriendModel&gt; friends) { if(ModelState.IsValid){ </code></pre> <p><strong>EDIT</strong> My View looks like this:</p> <pre><code>@model IEnumerable&lt;Models.ReferAFriendModel&gt; @for(int i=0;i&lt;Model.Count();i++) { @Html.Partial("_ReferAFriend", Model.ElementAt(i)); } </code></pre> <p>The partial looks like this:</p> <pre><code>@model Models.ReferAFriendModel &lt;p&gt; @Html.LabelFor(i =&gt; i.FullName) @Html.TextBoxFor(i =&gt; i.FullName)&lt;br /&gt; @Html.LabelFor(i =&gt; i.EmailAddress) @Html.TextBoxFor(i =&gt; i.EmailAddress) @Html.HiddenFor(i=&gt;i.Id) &lt;/p&gt; </code></pre> <p>When I post, I can see the fields are posted in the Request.Form object e.g Request.Form["FullName"] will show: "David Beckham","Thierry Henry". "Chicharito Fergurson" which are the values I entered in the form. <strong>But, the in the Post action,the value for 'friends' is always null.</strong> The ReferAFriendModel has three public properties Id, EmailAddress and FullName.</p> <p>What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload