0

I have 2 models, employee and person:

public class Employee
{ 
    [Key]
    public int Id { get; set; }
    public int? PersonId { get; set; }

    [ForeignKey("PersonId")]
    public virtual Person Person { get; set; }

}

public class Person
{ 
    public IList<PhoneNumber> PhoneNumbers { get; set; }
    public int Id { get; set; }
    public string FName { get; set; }
    public string LName { get; set; }
    public Person()
    {
        PhoneNumbers = new List<PhoneNumber>
                            {
                                new PhoneNumber()
                            };      
    }
}

Editor Template for Phone:

         @Html.TextBoxFor(x => x.Number)
         @Html.DropDownListFor(m => m, new SelectList(Enum.GetNames(typeof (WebMVC.Core.Common.PhoneType))))

To reduce clutter, I removed the other (non-pertinent) properties.

The difficulty I am having is while in the Employee Create(), I can bind the person FName & LName, I cannot bind the PhoneNumbers collection.

I know about the 2008 Haack blog but I do not think it mirrors this situation.

Does anyone know a solution to bind the person phone numbers collection in the employee's Create()?

4
  • What do you mean when you say you "cannot bind" them in the Create view? Does it simply not display the phone numbers property? Does it display them, but not bind in the save action? Provide more details. Commented Jun 3, 2012 at 15:09
  • The property names relative to the phone number class do appear in debugging (ie number, phone type), but their values are null. Moreover, it does not matter how many phone numbers I add (via add new phone), the collection is still 1, and as I said before all values null. Commented Jun 3, 2012 at 15:14
  • What html is being produced by your editor view code? What parameter name/value pairs are being sent to the save action? Commented Jun 3, 2012 at 15:36
  • Perhaps it would be easier if you just offered your recommended code? Commented Jun 3, 2012 at 15:42

2 Answers 2

2

I'm not exactly sure if PhoneNumber is a custom class that you created, or one that is built into the framework. But if you're having problems with MVC3 mapping posted data to the Employee class like you specified, you might want to look at creating a custom binding. Keep in mind that if your editor template code is incorrect this wont really matter, so I would take a look at that using fiddler first.

Here are a few good sites to get you started, I found them all on SO at one point. http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx http://odetocode.com/blogs/scott/archive/2009/05/05/iterating-on-an-asp-net-mvc-model-binder.aspx http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx

Creating a custom binder gives you complete control over the way that MVC parses your posted model data and populates the object. There are 2 main functions that most people override, CreateModel and BindModel. BindModel is the function you will most likely want to override if this is the way you would like to go.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't know what the html from the editor template looks like, but to bind to a collection of custom types it should look something like this:

<input name="[0].Number">
<input name="[0].PhoneType">
<input name="[1].Number">
<input name="[1].PhoneType">
<input name="[2].Number">
<input name="[2].PhoneType">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.