Sign in to follow this  
Krisc

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

Recommended Posts

Krisc    494
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 this post


Link to post
Share on other sites
Telastyn    3777
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.]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this