views:

174

answers:

1

How do I get properties in my BLL passed down to a ModeView. For example, I have this class in a separate Class Library:

[MetadataType(typeof(PersonMetaData))]
public partial class Person
{        
    [Bind(Include = "PersonId,DepartmentId,FirstName,LastName,Active,DateAdded,DateDeleted")]
    public class PersonMetaData
    {           
        public object PersonId { get; set; }

        public object DepartmentId { get; set; }

        public object FirstName { get; set; }

        public object LastName { get; set; }

        public Department PersonDepartment { get; set; }

        public string FullName()
        {
            return string.Format("{0} {1}", FirstName, LastName);
        }
    }
}

My ViewModel looks like this:

public class PersonViewModel
{
    public int PersonId { get; set; }
    public string FullName{ get; set; }
    public string PersonDepartment { get; set; }
}

When I generate a new "View" strongly-typed to the PersonViewModel and set as "List" View Content....the page is generated, but FullName is not coming through.

I created the PersonDepartment property because I want to display the Department Name the person is in. I have a Department Class set up similarly. For example, I want to be able to do something like "PersonDepartment.DepartmentName" that displays the department name on the page.

I am using a DBML (Linq To SQL), so the partial classes are extending from the auto-generated classes.

I am not sure how to get FullName property filled and passed to ViewModel and get Department properties connected to the Person information being passed. Any help would be greatly appreciated.

+1  A: 

You have mentioned that you are using AutoMapper. In your model FullName is a method and not a property. AutoMapper won't map automatically to methods. According to the conventions you could prefix your method name with Get to make this to work:

public string GetFullName()
{
    return string.Format("{0} {1}", FirstName, LastName);
}

This will be mapped to the FullName property in the view model. Another option is to explicitly declare how the mapping is done:

Mapper.CreateMap<Person, PersonViewModel>()
      .ForMember(
          dest => dest.FullName, 
          opt => opt.MapFrom(src => src.FullName())
      );

As far as the department name property is concerned I would recommend you modify your model so that instead of a DepartmentId property it has directly a property called Department containing the id and the name which will allow you to map them easily in your view model. If you cannot modify your model this way you could have the Department property in the view model populated directly by the repository and not by AutoMapper.

Darin Dimitrov