SureĀ Record and part here:
public class ImageAttachmentPartRecord :ContentPartRecord
{
public virtual ContentItemRecord Image { get; set; }
}
public class ImageAttachmentPart : ContentPart<ImageAttachmentPartRecord>
{
private readonly LazyField<IContent> _image = new
LazyField<IContent>();
public LazyField<IContent> ImageField { get; set; }
public IContent Image
{
get { return _image.Value; }
set { _image.Value = value; }
}
}
Migration:
public int UpdateFrom4() {
SchemaBuilder.CreateTable("ImageAttachmentPartRecord",
table => table
.ContentPartRecord()
.Column<int>("Image_Id")
);
ContentDefinitionManager.AlterPartDefinition(
"ImageAttachmentPart", builder => builder.Attachable());
return 5;
}
For good measure here's the handler & driver:
public class ImageAttachmentPartHandler : ContentHandler
{
private readonly IContentManager _contentManager;
public
ImageAttachmentPartHandler(IRepository<ImageAttachmentPartRecord>
repository, IContentManager contentManager)
{
Filters.Add(StorageFilter.For(repository));
_contentManager = contentManager;
OnInitializing<ImageAttachmentPart>(PropertySetHandlers);
OnLoaded<ImageAttachmentPart>(LazyLoadHandlers);
}
private void LazyLoadHandlers(LoadContentContext context,
ImageAttachmentPart part)
{
part.ImageField.Loader(() =>
part.Record.Image == null ?
null : _contentManager.Get(part.Record.Image.Id));
}
private static void PropertySetHandlers(InitializingContentContext
context, ImageAttachmentPart part)
{
part.ImageField.Setter(image =>
{
part.Record.Image = image == null
? null
: image.ContentItem.Record;
return image;
});
if (part.ImageField.Value != null)
part.ImageField.Value = part.ImageField.Value;
}
}
public class ImageAttachmentPartDriver :
ContentPartDriver<ImageAttachmentPart> {
private readonly IImageLibraryService _imageService;
private const string TemplateName = "Parts/ImageAttachment";
public ImageAttachmentPartDriver(IImageLibraryService imageService)
{
_imageService = imageService;
}
protected override string Prefix
{
get
{
return "ImageAttachment";
}
}
protected override DriverResult Editor(ImageAttachmentPart part,
dynamic shapeHelper)
{
return ContentShape("Parts_ImageAttachment_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: TemplateName,
Model: BuildEditorViewModel(part),
Prefix: Prefix
));
}
protected override DriverResult Editor(ImageAttachmentPart part,
Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper) {
var model = new EditImageAttachment();
updater.TryUpdateModel(model, Prefix, null, null);
if (part.ContentItem.Id != 0) {
_imageService.UpdateImageAttachmentForContentItem(part.ContentItem, model);
}
return Editor(part, shapeHelper);
}
private EditImageAttachment BuildEditorViewModel(ImageAttachmentPart
part) {
var model = new EditImageAttachment {ContentId =
part.ContentItem.Id};
if (part.Image != null) {
model.ImageId = part.Image.Id;
model.FileName
=part.Image.ContentItem.As<ImagePart>().Record.FileName;
model.ThumbUrl =
_imageService.GetUrl(part.Image.ContentItem.As<ImagePart>(), "thumb");
}
return model;
}
}
From: Bertrand Le Roy <[email protected]>
Reply-To: <[email protected]>
Date: Tue, 1 Feb 2011 23:02:49 +0000
To: <[email protected]>
Subject: RE: ContentPart not saving/updating
Can you show the record and migration maybe?
From: Chris Bower [mailto:[email protected]]
Sent: Tuesday, February 01, 2011 2:53 PM
To: [email protected]
Subject: ContentPart not saving/updating
Hey all,
I've got a content part that is a relation to another content item similar
to the SponsorPart in the RelationExample project. I pretty much followed
that example exactly, yet when I hit save on the content item I'm editing,
the linking field is not getting saved. In my service I can see the field
(part.Image) getting set properly here:
publicvoid UpdateImageAttachmentForContentItem(ContentItem contentItem,
EditImageAttachment model)
{
var part = contentItem.As<ImageAttachmentPart>();
if (model.ImageId != null)
{
part.Image = _contentManager.Get(model.ImageId.Value);
}
else
{
part.Image = null;
}
}
Yet when the editor refreshes the field is no longer set. I check in the
database and there is a record for my content part, but only the parent
content item id is set. The related content id is NULL.
What could I be doing wrong?
---
You are currently subscribed to orchard-discuss as:
[email protected].
To unsubscribe send a blank email to
[email protected].
---
You are currently subscribed to orchard-discuss as: [email protected].
To unsubscribe send a blank email to
[email protected].
---
You are currently subscribed to orchard-discuss as: [email protected].
To unsubscribe send a blank email to [email protected].