开发者

Cannot obtain values from the View (with MVC code first)

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">
            @Htm开发者_运维问答l.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


The default model binder can only bind fields to simple properties on your model. You need to implement your own model binder.

Check it out at: http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜