This project is read-only.

Updating IEnumerable or ViewModel on Driver

Topics: Writing modules
Aug 23, 2013 at 8:52 AM

I am trying to create a settings page on Orchard which consists of several kind of mappings for my module. The way I'm doing it is I'm passing an IEnumerable consisting of my model to the ContentShape
protected override DriverResult Editor(CRMMappingsPart part, dynamic shapeHelper)
            var mappings = _contentManager.Query<CRMMappingsPart>().List().ToList();

            return ContentShape("Parts_CRMMapping_Settings", () => shapeHelper.EditorTemplate(TemplateName: "Parts.CRMMapping.Settings", Model: mappings));
It displays fine and I get all the mappings I need but I am a little lost on saving the edited values on POST.

relevant code on the front end
@foreach (var mapping in mappings)
            <label for="@Html.FieldIdFor(m => mapping.PersonField)">@mapping.PersonField</label>
            @Html.DropDownListFor(m => mapping.Property, new SelectList(properties, mapping.Property))
How does one go about POSTing changes in this kind of scenario? or when using a ViewModel?
Aug 23, 2013 at 8:49 PM
Not sure I understood what you're asking. Almost all drivers have two Editor methods, on of which is handling the post.
Aug 23, 2013 at 10:15 PM
I'm not sure what the question is but you may find this blogpost useful.
Dec 10, 2013 at 11:04 PM
He's asking what do you actually DO in the 2nd Editor method? That method takes a part, an IUpdateModel, and a shapeHelper. How do you get the relevant fields out of the ViewModel and into the part to call TryUpdateModel? Is there a convention there that I'm missing?
Dec 11, 2013 at 12:38 AM
Still not clear. What you do in the second method typically is call TryUpdateModel on an object you want to update with the post data. That's typically all you have to do.
Dec 11, 2013 at 4:17 PM
Ah, see that makes sense now. I thought TryUpdateModel was for updating the database, but it's actually for updating your model from the post data. That's what I needed to know!
Dec 11, 2013 at 5:26 PM
Ah yes, persistence is usually transparently taken care of for you and rarely needs to be done explicitly.