• 14
• 14
• 9
• 10
• 9

# [web] [MVC 2] Editing composite types with child complex properties?

This topic is 2913 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I posted this question on stack overflow and got an answer that was somewhat satisfactory. But the answer wasn't very in depth. I attempted to get the ViewModel solution to work, but it was met with an error. Essentially I have a model object like so...
public class Entry{
public User Author { get; set; }
}
...where the User property "Author" should be represented as a drop down of users only when editing the Entry item. I have been able to get the dropdown to appear when using the ViewModel pattern, but the reverse process doesn't seem to happen because MVC can't turn the string value into a User object. This is obvious to me, because the object isn't stored in the HTML, but I feel as though I am missing a crucial step. If the ViewModel attempts to abstract the back-and-forth code necessary to represent the User collection as a dropdown, that is fine albeit a little unnecessary. I had previously thought the ViewModel pattern was used for cases like this and allowed for somewhat of an automagic way of editing composite types with complex child properties. My ViewModel has essentially been the following:
public class EntryViewModel{
public EntryViewModel(){
Users = new SelectList( /* fill with users from EDM */ );
}

public Entry Entry { get; set; }

public SelectList Users { get; set; }
}