2
votes

I created a content part and then added it to a content type in Orchard. But when I try to create a content item of that type, the fields for the part's properties are not displayed. I'm looking for suggestions of where the problem might be.

UPD: the relevant code:

using JetBrains.Annotations;
using ArealAds.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using ArealAds.Models;
using ArealAds.Services;
using ArealAds.ViewModels;

namespace ArealAds.Drivers {
    [UsedImplicitly]
    public class AdDriver : ContentPartDriver<AdPart> {
        private readonly IAdService _adService;

        public AdDriver (IAdService adService)
        {
            _adService = adService;
        }

        protected override string Prefix {
            get { return "AdPart"; }
        }

        protected override DriverResult Display(
            AdPart part, string displayType, dynamic shapeHelper) {

            return ContentShape("Parts_Ad", () => shapeHelper.Parts_Ad(
                Title: part.Title,
                Url: part.Url,
                Email: part.Email,
                Phone1: part.Phone1,
                Phone2: part.Phone2,
                AreaName: part.AreaRecord.Name,
                AreaId: part.AreaRecord.Id,
                DistrictName: part.DistrictRecord.Name,
                DistrictId: part.DistrictRecord.Id,
                AllDistricts: part.AllDistricts));
        }

        //GET
        protected override DriverResult Editor(
            AdPart part, dynamic shapeHelper) {

            return ContentShape("Parts_Ad_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/Ad",
                    Model: BuildEditorViewModel(part),
                    Prefix: Prefix));
        }
        //POST
        protected override DriverResult Editor(
            AdPart part,
            IUpdateModel updater,
            dynamic shapeHelper) {

            var model = new EditAdViewModel();
            updater.TryUpdateModel(model, Prefix, null, null);

            if (part.ContentItem.Id != 0) {
                _adService.Update(
                    part.ContentItem, model);
            }

            return Editor(part, shapeHelper);
        }

        private EditAdViewModel BuildEditorViewModel(AdPart part) {
            var avm = new EditAdViewModel {
                Title = part.Title,
                Url = part.Url,
                Email = part.Email,
                Phone1 = part.Phone1,
                Phone2 = part.Phone2,
                AllDistricts = part.AllDistricts,
                Areas = _adService.GetAreas(),
                Districts = _adService.GetDistricts()
            };
            if (part.AreaRecord != null) {
                avm.AreaName = part.AreaRecord.Name;
                avm.AreaId = part.AreaRecord.Id;
            }
            if (part.DistrictRecord != null) {
                avm.DistrictName = part.DistrictRecord.Name;
                avm.DistrictId = part.DistrictRecord.Id;
            }
            return avm;
        }
    }
}

using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;

namespace ArealAds.Models {
    public class AdRecord : ContentPartRecord {
        public virtual string Title { get; set; }
        public virtual string Url { get; set; }
        public virtual string Email { get; set; }
        public virtual string Phone1 { get; set; }
        public virtual string Phone2 { get; set; }
        public virtual AreaRecord AreaRecord { get; set; }
        public virtual DistrictRecord DistrictRecord { get; set; }
        public virtual bool AllDistricts { get; set; }
    }

    public class AdPart : ContentPart<AdRecord> {
        [Required]
        public string Title {
            get { return Record.Title; }
            set { Record.Title = value; }
        }

        public string Url {
            get { return Record.Url; }
            set { Record.Url = value; }
        }

        public string Email {
            get { return Record.Email; }
            set { Record.Email = value; }
        }

        public string Phone1 {
            get { return Record.Phone1; }
            set { Record.Phone1 = value; }
        }

        public string Phone2 {
            get { return Record.Phone2; }
            set { Record.Phone2 = value; }
        }

        public AreaRecord AreaRecord {
            get { return Record.AreaRecord; }
            set { Record.AreaRecord = value; }
        }

        public DistrictRecord DistrictRecord {
            get { return Record.DistrictRecord; }
            set { Record.DistrictRecord = value; }
        }

        [Required]
        public bool AllDistricts {
            get { return Record.AllDistricts; }
            set { Record.AllDistricts = value; }
        }
    }
}


@model ArealAds.ViewModels.EditAdViewModel

<fieldset>
  <legend>Area Fields</legend>

  <div class="editor-label">
    @Html.LabelFor(model => model.Title)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Title)
    @Html.ValidationMessageFor(model => model.Title)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Url)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Url)
    @Html.ValidationMessageFor(model => model.Url)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Email)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Email)
    @Html.ValidationMessageFor(model => model.Email)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Phone1)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Phone1)
    @Html.ValidationMessageFor(model => model.Phone1)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Phone2)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Phone2)
    @Html.ValidationMessageFor(model => model.Phone2)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.AllDistricts)
  </div>
  <div class="editor-field">
    @Html.CheckBoxFor(model => model.AllDistricts)
    @Html.ValidationMessageFor(model => model.AllDistricts)
  </div>

  <table>
  <tr>
  <td>
  <div class="editor-label">
    @Html.LabelFor(model => model.AreaId)
  </div>
  <div class="editor-field">
    @Html.DropDownListFor(model => model.AreaId,
                          Model.Areas.Select(s => new SelectListItem {
                              Selected = s.Id == Model.AreaId,
                              Text = s.Name,
                              Value = s.Id.ToString()
                          }),
                      "Выберите район...")
    @Html.ValidationMessageFor(model => model.AreaId)
  </div>
  </td>
  <td>или</td>
  <td>
  <div class="editor-label">
    @Html.LabelFor(model => model.DistrictId)
  </div>
  <div class="editor-field">
    @Html.DropDownListFor(model => model.DistrictId,
                          Model.Districts.Select(s => new SelectListItem {
                              Selected = s.Id == Model.DistrictId,
                              Text = s.Name,
                              Value = s.Id.ToString()
                          }),
                      "Выберите округ...")
    @Html.ValidationMessageFor(model => model.DistrictId)
  </div>
  </td>
  </tr>
  </table>

</fieldset>


            SchemaBuilder.CreateTable("AdRecord", table => table
                .ContentPartRecord()
                .Column<string>("Title")
                .Column<string>("Url")
                .Column<string>("Email")
                .Column<string>("Phone1")
                .Column<string>("Phone2")
                .Column<int>("AreaRecord_Id")
                .Column<int>("DistrictRecord_Id")
                .Column<bool>("AllDistricts")
            );

            ContentDefinitionManager.AlterPartDefinition(
                typeof(AdPart).Name, cfg => cfg.Attachable());

            ContentDefinitionManager.AlterTypeDefinition(
                "ArealAds_Ad", cfg => cfg
                .WithPart("CommonPart")
                .WithPart("AdPart")
                .Creatable()
            );
1
Can you post the code for the part model, record, driver, and handler?Giscard Biamby
@GiscardBiamby I posted the code. Thank you for helping - again :) By the way, from your experience, are such problems common among the Orchard beginners, or is it just me? I kind of begin to worry about my skills after spending about an hour trying to find an error in my code and just not being able to spot it <_<Alexei Averchenko
These problems are common. I ran into a lot of the same ones that others ask about, and I still run into issues when I try new features.Giscard Biamby
Can you the ViewModel(s), and the code related to AreaRecord and DistrictRecord (including models, migration, etc). Are you sure those error logs you pasted are related to this issue? If you clear the logs , restart the development web server, and load the editor page again do you still get the error in the logs?Giscard Biamby
Also, have you tried to run the project in debug mode while loading the editor page?Giscard Biamby

1 Answers

5
votes

You're probably missing an entry in placement.info.

Add this into the placement.info file in your module (not the placement.info for yoru theme, since the theme is not active while you're in the dashboard):

`<Place Parts_Ad_Edit="Content:1" />`