This is how I populate the view from the viewmodel PersonInformation..
@model Client.Models.PersonInformation
<fieldset>
<legend>Contact Information</legend>
@foreach (var item in Model.MemberContacts)
{
<div class="editor-label">
@Html.DisplayFor(model => item.MemberContactDescription)
</div>
<div class="editor-field">
@Html.EditorFor(model => item.ContactInformation)
@Html.ValidationMessageFor(modelitem => item.MemberContactDescription)
</div>
}
</fieldset>
And the ViewModel
public class PersonInformation
{
public Person person { get; set; }
public Address premiseAddress { get; set; }
public Address billingAddress { get; set; }
private ICollection<MemberContact> _MemberContacts { get; set; }
public virtual ICollection<MemberContact> MemberContacts
{
get { return _MemberContacts ?? (_MemberContacts = new HashSet<MemberContact>()); }
set { _MemberContacts = value; }
}
public PersonInformation()
{
BIMemberService lib = new BIMemberService();
ICollection<MemberContact> mcs = new List<MemberContact>();
foreach (ContactLib itm in lib.ContactLibs())
{
MemberContact mc = new MemberContact();
mc.MemberContactDescription = itm.Description;
mc.ContactLibID = itm.ContactLibId;
mc.ContactInformation = "test";
mcs.Add(mc);
MemberContacts.Add(mc);
}
}
}
But this one (which has an input text field), do not have a value on the post action.
@Html.EditorFor(model => item.ContactInformation)
This is my post action.
[HttpPost]
public ActionResult PersonInformation(PersonInformation member)
{
if (ModelState.IsValid)
{
db.Apply(member);
return View("Requirements", member.person.MemberRequirements);
}
return View(member);
}
Tracing down the code, the line below produces null value
member.MemberContacts.ContactInformation
Any advise, is highly appreciated.
Thanks
You are trying to use editor for with an arbitrary item of a collection.
try reading this post. It should give you some insight on collection binding