1
votes

I'm very new to Orchard (and ASP.NET MVC) and I'm having some difficulty wrapping my head around how Orchard part properties can be automatically set in a controller. I have a "Gallery" content type, consisting of the stock Title, Container and Owner parts. I'm creating a controller with a Create method that hides all of the details of the Container part since I'm going to set the default page length, the item type, et cetera in the HttpPost version of the Create method. My problem is I don't know how to set those values on the http Post.

My general plan was as follows:

I created a CreateGalleryViewModel containing the title and admin username, both as a string. IN the view those two are represented as form fields. On the submit, I could run TryUpdateModel in the controller to update the CreateGalleryViewModel with the form values. But then how do I update the Part properties? I think I'm supposed to run IContentManager.UpdateEditor(, this) but I don't think this ca, work because I never ran IContentManager.BuildEditor in the first place. Here's where I'm stuck because I don't see how the content parts get validated and updated properly.

Am I looking at this at the wrong level of abstraction? How do I make a simplified editor for my Gallery content type?

1
you mentioned that you haven't used BuildEditor to create form.can i ask why you not used BuildEditor , and which method you have used instead?Behnam Esmaili
@BehnamEsmaili -The reason I did was because BuildEditor would create a form with all of the parts and form field, including the ones I don't want, such as the container's item type, etc. I'm open to a different way though.wwahammy
please post code for CreateGalleryViewModel parts (or properties) you wanna update and ContentType definition for Gallery (including parts).Behnam Esmaili

1 Answers

1
votes

What you basically need to know here is the following:

  • You can build the editor of a content item with IContentManager.BuildEditor()
  • You can updated an item's parts' values from the POST data with IContentManager.UpdateEditor()
  • You can also access parts by "casting" content items with the As() method (needs the Orchard.ContentManagement namespace).
  • When you're dealing with content items because of their dynamic nature it's rarely a good approach to create a view model where you recreate some of the parts' properties. If you have such static view models for what's contained in a content item then you'll miss the extensibility and flexibility that comes with Orchard's content model. E.g. if you add a new part to that content type since you're using a static view model the new part won't be handled.

For a complete example of how to manage content items from code, see the Training Demo module.