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

## 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; }
}

##### Share on other sites
IIRC, you should be able to set a UIHint attribute on the Author property in Entry to cause it to use a different EditorFor. Unfortunately I can't find the article I saw that in.

Or you could just inherit from User and make a different EditorFor for the new type. I think... I don't have too much experience with mvc2, and the documentation is still pretty sparse.

[edit: this seems like a step in the right direction.]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627702
• Total Posts
2978712

• 21
• 14
• 12
• 10
• 12