Orchard CMS works with MediaPickerField defined in Migrations.cs

MediaPickerFields are still eluding me.

I defined a new part in Migrations.cs and added the column Booleanand MediaPickerFieldto the part as follows:

SchemaBuilder.CreateTable("ImageContentPartRecord", table =>
            table.ContentPartRecord()
            .Column("DisplayImage", DbType.Boolean));

ContentDefinitionManager.AlterPartDefinition("ImageContentPart", builder =>
            builder
                .Attachable()
                .WithField("ImageField", fld =>
                    fld.OfType("MediaPickerField")
                    .WithDisplayName("Image")));

Assuming I have classes ImageContentPartand ImageContentPartRecordhow can I get data from my MediaPickerField (url, sizes, text, class, etc.) in my driver and in my part templates (Edit / Display)?

i.e. - Parts.ImageContent.cshtml (I want to do something similar):

<div>
     <img src="@Model.ImageField.Url" alt="@Model.ImageField.Alt" />
</div>

Any ideas?

+5
source share
1 answer

Here is the solution:

In my Display Driver method, I have to pass my ImageContentPart( part) when creating ContentShape:

return ContentShape("Parts_ImageContent", () =>
            shapeHelper.Parts_ImageContent(
                Content: part));

Orchard MediaPickerField. .ContentItem , , (.ImageContentPart) (.ImageContent), .

@{
    // Attempting to access MediaPickerField named 'ImageContent'
    var image = Model.Content.ContentItem.ImageContentPart.ImageContent;

    // Leaning on Orchard dynamics to access properties of the field
    var url = image.Url;
}

<img src="@url" alt="@T(image.AlternateText)" />

MediaPickerField \\Orchard.Fields\Views\Fields\MediaPicker.cshtml:

@* 
    Alternate Text: @Model.ContentField.AlternateText
    Class: @Model.ContentField.Class
    Style: @Model.ContentField.Style
    Alignment: @Model.ContentField.Alignment
    Width: @Model.ContentField.Width
    Height: @Model.ContentField.Height
    Url: @Model.ContentField.Url


    You can also display an image using this example, and add the attributes you need:
    <img src="@Href(Model.ContentField.Url)" />
*@

, , !

+4

All Articles