Search code examples
asp.net-mvcentity-frameworkef-code-first

Model - field based on values in other fields


I've literally just started learning MVC.

I have created a simple model:

public class StaffMember
{
    public Guid StaffMemberId { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Team { get; set; }
    public virtual ICollection<Observation> Observations { get; set; }
}

Now I have decided that I want to include a drop down list of all StaffMembers on the create page for the observation records. I manage to do that with the following code:

@Html.DropDownListFor(o => o.StaffMemberId,
                          new SelectList(Model.StaffMembers,
                                         "StaffMemberId", 
                                         "Forename", 
                                         Model.StaffMemberId), 
                          "-- Select Staff Member --")

This works perfectly, although, you'll notice that I can only include a single field, "Forename".

I want the drop down list to show the staff member's full name. I tried concatenating the fields manually i.e. "Forename" + " " + "Surname" but that threw and exception about there being no such field as "Forename" + " " + "Surname".

My question is this - is it possible to add to my model some sort of property that is simply based on the value of two existing properties. Something like:

public class StaffMember
{
    private string _fullName;

    public Guid StaffMemberId { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Team { get; set; }
    public virtual ICollection<Observation> Observations { get; set; }

    public string FullName
    {
        get
        {
            return _fullName;
        }
        set
        {
            value = this.Forename + " " + this.Surname;
            _fullName = value;
        }
    }
}

I tried the above, but when populating my database (I'm using entity model code first), that field always has a value of null, even though the field shows the correct value when debugging.

I'm using the following to auto populate the db with some test data:

var staff = new List<StaffMember>
{
    new StaffMember
    {
        Forename = "Bob",
        Surname = "Hope",
        StaffMemberId = Guid.NewGuid(),
        Team = "Test" 
    },
    new StaffMember
    {
        Forename = "Stan",
        Surname = "Laurel",
        StaffMemberId = Guid.NewGuid(),
        Team = "Test"
    }
};

staff.ForEach(s => context.StaffMembers.Add(s));
context.SaveChanges();

Any pointers would be really useful here, especially if I am approaching this in completely the wrong way!


Solution

  • Yes, you're really close with the FullName property.

    public class StaffMember
    {
        public string FullName
        {
            get
            {
                return this.Forename + " " + this.Surname;
            }
        }
    }
    

    No need for a private _fullName since you only need to get the values of Forename and Surname. And you don't need a set since you won't set a value back to this model using FullName