This project is read-only.

Modeling IEnumerable and Adding Items to a List in the Editor Template

Topics: Writing modules
Apr 13, 2011 at 3:13 PM
Edited Apr 13, 2011 at 3:18 PM

I am trying to create a content type that has a list of related links. If I was using a straight MVC project, this would be easy, I would have a IEnumerable<Link> member of a class, and on the add page for the object, an Ajax method for adding a new link. 

I've read the 1-N documentation on the site, but I'm still not grokking how to model my classes and implement the needed editor functionality. 



Apr 13, 2011 at 3:26 PM


I'm writing a module that enables n:n relationships between content items in a completely flexible manner and yes there are some confusing points.

In your record class you can specify strongly-typed references to other record classes (and the migrations codegen will create the appropriate id fields for you in the database).

There are some examples in the Orchard source where this happens but they're few and far between.

My module isn't quite ready although I've been doing some refactoring yesterday and today and I have something semi-workable. "Related Links" or just "Related Content" are the kind of use cases I had in mind (beyond my own needs). So once that's ready it would be ideal for you, but until then that's the way to reference tables. Let me know if you need further pointers.

It sounds like you're also unfamiliar with ContentParts and editor templates. You'd need to make a RelatedLinksPart and that allows you to push both editor templates and view templates into the content item shape. Look up some tutorials on building ContentParts, and you might also want the Advanced Code Generation module which makes the whole process much easier:

Apr 13, 2011 at 4:08 PM
Edited Apr 13, 2011 at 4:08 PM

I'm familiar with basic content parts and editor templates, that much I get. What I don't get yet is how to make a dynamic update template where I can have a text box with it's own "add" button, independent of the save button, that would add a link to my list. Or even if I should do that at all. What I would need (I suspect) is a controller action that takes an id and a string, adds that to the list of links for that item.

I keep feeling on the edge of getting it, there's just a lot to ingest first.


Apr 13, 2011 at 4:41 PM

Well you can write your own controller to perform the link adding and return a partial view displaying the updated list of links. You can use normal MVC ajax in your editor view to call the controller from the button and perform the update. That sounds like probably the simplest route to getting what you want.