0

So I have a View Model like this:

Public Class CategoryViewModel Public Property ID As Integer Public Property Name As String Public Property Display As Boolean Public Property DisplayIndex As Integer End Class 

And then this:

Public Class CategoriesViewModel Public Property Categories As IEnumerable(Of CategoryViewModel) End Class 

How do I map a collection of categories (Domain.Category) to the Categories (CategoryViewModel) property in the CategoriesViewModel?

Also, how would you handle a collection of Sub Categories? Would you make another View Model for that and have a collection of those on the CategoryViewModel?

1 Answer 1

1

Just tell automapper how the types should map, so tell AM how Category maps to CategoryViewModel. AM doesn't care if you map an array of categories to a list of categoriesviewmodel as long as you tell it how to map the types.

For the second part are sub categories self referencing?

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

7 Comments

@Derek - Self Referencing? Sorry, I am new to this, so what does that mean?
Does you Category class have a collection of Category.
@Derek - No, I have Categories that have Sub Categories.
So, no I wouldn't make another viewmodel for that. One suggestion name your view models based on what they do like CategoryDisplay or CategoryInput. So in your case my CategoryDisplay view model would have a collection of Category and Category would have a collection os SubCategory. In some cases your database model entities might be 1 to 1 mappings to your view model but that's ok. Check out ASP.NET MVC in Action 2, it's an excellent resource.
@Derek - So if you do not have a Model for each entity, then what do you use? The POCO classes? I was under the impression you did not want to use domain objects in your view/model, but rather a View Model per entity. - Thanks!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.