My ViewModel has a property that is a collection of another of my model entities, in this case CategoryTags (and each tag has a Tag and an ID).
1) Am I correct in understanding that Html.EditorFor() doesn't have an UI it can create for an ICollection?
2) Assuming #1, I've decided to make an EditorTemplate that is a textbox where my user can key in comma-separated tag names and jquery will autocomplete. Will I have to pass back the list of tag names (or their respective IDs) and then parse them back into the CategoryTags propety on POST?
Thanks!
- Correct - you need your own template (See http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3)
- Yes this can work - the model binder will populate properly if they are named the right thing id[0], etc. see Phil Haack's article on this: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Edit for #2 - I read your original post a little incorrectly. If you put them all into a single textbox, then you will need to parse them (or create your own model binder). You could have an editor that adds new textboxes to the DOM for each item and then model binder will correctly repopulate the list. However in the case you mentioned, your options are manually do this on the server, or use your own model binder (which would be fairly easy here) to break them apart into a model. Either or : )
来源:https://stackoverflow.com/questions/5756927/editorfor-collection-of-items-in-my-model